willcascadeondelete in .netMVC4 is not working - asp.net-mvc

I am creating .net MVC4 application. i have two entitites named group and groupmembers. group has a one to many relations with groupmembers.
Here are my classes
public class Group
{
public int GroupId{get;set;}
public virtual ICollection<GroupMember> Members { get; set; }
}
public class GroupMember
{
public int GroupMemberId { get; set; }
public int GroupId { get; set; }
public virtual Group Group { get; set; }
}
on the model side i am using the code
modelBuilder.Entity<GroupMember>()
.HasRequired(e => e.Group)
.WithMany()
.HasForeignKey(e => e.GroupId)
.WillCascadeOnDelete();
My problem is while deleting the group the members of the group is not getting deleted.Can any one please give me a solution

The WillCascadeOnDelete() tells EF that the database will perform the delete. EF does not perform the cascade, so you need to change your tables to add the delete.
You can do this in SSMS designer by opening the relationship selecting Delete for Cascade Action, or you can do it in T-SQL:
ALTER TABLE dbo.GroupMember DROP CONSTRAINT fkName
GO
ALTER TABLE dbo.GroupMember ADD CONSTRAINT
fkName FOREIGN KEY (GroupId)
REFERENCES dbo.Group (Id) ON DELETE CASCADE

Related

How to configure a many to many relationship with primary key too in it

This is what i am trying to achieve :
modelBuilder.Entity<ApplicationUser>()
.HasMany(u => u.Following)
.WithMany(u => u.Followers)
.Map(m =>
{
m.ToTable("FollowTables");
m.MapLeftKey("UserId");
m.MapRightKey("FollowId");
});
In application user class, i have configured following and followers like this:
public ICollection<ApplicationUser> Following { get; set; }
public ICollection<ApplicationUser> Followers { get; set; }
follow table should be something like this:
public class FollowTable
{
[Key]
public int autoId { get; set; }
public int UserId { get; set; }
public int? FollowId { get; set; }
}
autoId is primary key and UserId and FollowId both are foreign key to ApplicationUser class where UserId is user's own id and FollowId are the ids which user is following.Its data could be following:
autoId UserId FollowId
1 4 11
2 4 12
3 4 13
Now, i problem is when i update database through pmc, it is creating two database tables one is FollowTables with column (USerId, FollowId) and one is FollowTables1(autoId, USerId, FollowId).
If i remove this line from applicationDbContext class:
public DbSet<FollowTable> FollowTables { get; set; }
then its creating only one table but with no primary key.
please someone help me out . how to properly configure UserId and followId as foreign key and these two should map to ApplicationUser's Id.
I want to use those Collection's following and Followers too.how to do it.
You have to decide if you want to work with an entity that represents the junction table or not. If you don't need to add any other properties to that table, excluding the FKs, then I suggest you don't map the junction table as entity. It is going to be more easy for you due to Entity Framework will handle that table for you.
Now if you really need to map that table then you need to delete many-to-many fluent api configuration and change the type of your navigation properties:
public ICollection<FollowTable> Following { get; set; }
public ICollection<FollowTable> Followers { get; set; }
That is going to create two one-to-many relationships with the junction table, an explicit representation of a many-to-many relationship. To do that you also need to do some changes in that entity:
public class FollowTable
{
[Key]
public int autoId { get; set; }
public int UserId { get; set; }
[ForeignKey("User")]
public ApplicationUser User{ get; set; }
[ForeignKey("Follow")]
public int? FollowId { get; set; }
public ApplicationUser Follow{ get; set; }
}
Also, I don't think FollowId Fk property should be a nullable FK, because you want to represent a relationship between two persons.
If you ask my opinion about what option you should take, I suggest you don't map the junction table if you are going to have only those properties.

Remove primary key in entity framework table field in mvc4

I have created a model in the name of Sample with an Id and DocumentId property.In this I don't mention primary key for Id property.But it forms the primary key when I created the Sample as table in entity framework.I want to remove the primary key for Id. What do I have to do. Please help me. I am very new to mvc4.
public class Sample
{
[Required,DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
[Required]
public int DocumentId { get; set; }
}
public override void Up()
{
CreateTable(
"dbo.Samples",
c => new
{
Id = c.Int(nullable: false, identity: false),
DocumentId = c.Int(nullable: false),
})
.PrimaryKey(t => t.Id);
}
I am putting this here as I don't think it will show up very well in comments :)
In the case of a user having many roles (and each role possibly being played by many users), you would have three tables linked to 2 classes. The tables would be a Users table with a unique UserId column and the rest of the user details. Another table would be Roles with a unique RoleId and the rest of the role information and a joining table that would have the id of the user and the id of the role he plays (this table could have a unique id of itself). If the user has 2 roles, they would have 2 records in the joining table, one for each of the roles. The classes would look something like this:
public class User{
public long UserId {get;set;}
public ICollection<Role> roles{get;set;}
//Other properties of the user name, DOb,etc.
}
public class Role{
public long RoleId{get;set;}
public ICollection<User> Users{get;set;}
//other properties of Role
}
This is a many to many relationship. Of course you could also have it as a one to many relationship if the role can be played by one user. In that case you don't need the joining table and you can just add a UserId column to the Role table and instead of a collection of users, the role would have a single property of type user (not really needed unless you want to navigate back from role to user).
Try adding the NotMapped attribute in your Id.
public class Sample
{
[Key]
public int SampleId {get;set;}
[Required,DatabaseGenerated(DatabaseGeneratedOption.None)]
[NotMapped]
public int Id { get; set; }
[Required]
public int DocumentId { get; set; }
}
EDIT:
I added a key attribute to specify your primary key.
You can also try this, which I think is better:
public class Sample
{
public int SampleId {get;set;}
public virtual ICollection<Document> Documents {get;set;}
}
public class Document
{
public int DocumentId {get;set;}
public int SampleId {get;set;}
public virtual Sample Sample {get;set;}
}
virtual keyword is for lazy loading.
Open the entity diagram (.edmx) file and find which ever property in that particular table is the primary key. Right click on it and select "properties". In the properties window see where it says StoreGeneratedPattern - Identity? Change that to none and save the diagram. It will regenerate the .cs model files by itself
Fluent API suppresses the Data Annotations in Entity framework. Data Annotation for Primary is [Key] and ID is by default Primary key with identity.
In that scenario, Delete(if it's there) Data Annotation of ID i:e;[KEY] and use Fluent API in your context class. In below example, my primary key is "CustomerName" due to Fluent. Example:
public class Customer
{
public int CustomerName { get; set; }
public int ID { get; set; }
[Required]
public string ProductID { get; set; }
}
In Context Class:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().HasKey(s => s.CustomerName);
}

Code first relationships with entity framework, fluent API

I have a legacy table I need to connect my app to. I am using a code-first, POCO model. I have the following classes:
public class Equipment
{
[Key]
public string EquipmentId { get; set; }
public string OriginatorId { get; set; }
public virtual Employee Employee { get; set; }
}
public class Employee
{
[Key]
[Column("employee_id")]
public string EmployeeId { get; set; }
public string EmployeeName { get; set; }
[ForeignKey("OriginatorEmployeeId")]
public virtual Equipment Equipment { get; set; }
}
I need to map EmployeeId in the Employee class to to OriginatorEmployeeId in the Equipment class.
Also, the legacy table is represented by the Employee class. The table is actually named employee (lower case) and the EmployeeId column is named "employee_id". I want to keep naming of my classes and properties consistent with the rest of the app, hence Employee and EmployeeId.
Here is what I have tried using fluent API:
modelBuilder.Entity<Employee>().Map(m =>
{
m.MapInheritedProperties();
m.ToTable("employee");
});
modelBuilder.Entity<Equipment>()
.HasOptional<Employee>(u => u.Employee)
.WithOptionalDependent(c => c.Equipment).Map(p => p.MapKey("OriginatorEmployeeId"));
I am probably mixing things I don't need to. The error I am getting right now is:
Multiplicity is not valid in Role 'Equipment_Employee_Source' in relationship 'Equipment_Employee'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
Any help is appreciated.
Can an employee record be associated to more than one equipment record? If they can then your Employee POCO should contain a collection property representing a one-to-many relationship between Employee and Equipment.
public virtual ICollection<Equipment> Equipments {get;set;}
You configuration should then be adjusted accordingly to show this relationship:
modelBuilder.Entity<Employee>()
.HasMany<Equipment>(u => u.Equipments)
.WithRequired(c => c.Employee).HasForeignKey(p => p.OriginatorId);
It also looks like you will need to setup a configuration for your column name mappings as well. Therefore, I would recommend that you create a separate configuration file for each of your POCOs to make it easier to manage the configurations, then just add those configurations to the modelbuilder.Configurations collection in your OnModelCreating event of your DBContext
public class EmployeeConfiguration : EntityTypeConfiguration<Employee>
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelbuilder.Configurations.Add(new EmployeeConfiguration());
}

Many to many relation scaffolded view generates error _id1

The application I'm working on generates an error while creating an instance of a model. I have Product and Color (many to many) and ProductImage (many ProductImage to a ProductColor).
public partial class ProductColor
{
public ProductColor()
{
this.ProductImages = new HashSet<ProductImage>();
}
public int Id { get; set; }
[DefaultValue(0),DisplayName("Price Offset")]
public Decimal PriceOffset { get; set; }
public int ProductId { get; set; }
public int ColorId { get; set; }
public virtual Color Color { get; set; }
public virtual Product Product { get; set; }
public virtual ICollection<ProductImage> ProductImages { get; set; }
}
public partial class ProductImage
{
public int Id { get; set; }
[DisplayName("File name"),
Required(),
StringLength(255)]
public string FileName { get; set; }
public bool Default { get; set; }
public int ProductColor_Id { get; set; }
public virtual ProductColor ProductColor { get; set; }
}
public class testContext : DbContext
{
public testContext() : base("name=testContext")
{
}
public DbSet<Product> Products { get; set; }
public DbSet<Manufacturer> Manufacturers { get; set; }
public DbSet<ProductColor> ProductColors { get; set; }
public DbSet<Color> Colors { get; set; }
public DbSet<ProductImage> ProductImages { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>()
.HasMany(c => c.ProductColors);
}
}
After scaffolding the controller and views for ProductImage and going to the index of ProductImage I get an error trying to get ProductImages from the db context.
No wonder because Entity decided the following sql should be used to get the instances:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[FileName] AS [FileName],
[Extent1].[Default] AS [Default],
[Extent1].[ProductColor_Id] AS [ProductColor_Id],
[Extent1].[ProductColor_Id1] AS [ProductColor_Id1]
FROM [dbo].[ProductImages] AS [Extent1]
ProductColor_Id1 does not exist in the database. Here is the sql that created the tables:
CREATE TABLE [dbo].[ProductColors] (
[Id] int IDENTITY(1,1) NOT NULL,
[PriceOffset] decimal(7,2) NOT NULL,
[ProductId] int NOT NULL,
[ColorId] int NOT NULL
);
CREATE TABLE [dbo].[ProductImages] (
[Id] int IDENTITY(1,1) NOT NULL,
[FileName] nvarchar(255) NOT NULL,
[Default] bit NOT NULL,
[ProductColor_Id] int NOT NULL
);
ALTER TABLE [dbo].[ProductColors]
ADD CONSTRAINT [PK_ProductColors]
PRIMARY KEY CLUSTERED ([Id] ASC);
ALTER TABLE [dbo].[ProductColors]
ADD CONSTRAINT [FK_ProductColorColor]
FOREIGN KEY ([ColorId])
REFERENCES [dbo].[Colors]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
CREATE INDEX [IX_FK_ProductColorColor]
ON [dbo].[ProductColors]
([ColorId]);
ALTER TABLE [dbo].[ProductColors]
ADD CONSTRAINT [FK_ProductColorProduct]
FOREIGN KEY ([ProductId])
REFERENCES [dbo].[Products]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
CREATE INDEX [IX_FK_ProductColorProduct]
ON [dbo].[ProductColors]
([ProductId]);
ALTER TABLE [dbo].[ProductImages]
ADD CONSTRAINT [FK_ProductColorProductImage]
FOREIGN KEY ([ProductColor_Id])
REFERENCES [dbo].[ProductColors]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
CREATE INDEX [IX_FK_ProductColorProductImage]
ON [dbo].[ProductImages]
([ProductColor_Id]);
The database is generated from an Entity diagram and looks fine to me. I have no idea why on a ProductImage create it added ProductColor_Id1 in the select statement.
Hope there is enough information provided and that this is a general mistake that's easily solved. Thank you for reading this and hope you can help.
I would like the scaffolded controller and views to work in listing, creating, editing and deleting the ProdcutImage objects but as it is it's not even possible to create one with the information provided to Entity.
After deleting and re creating the association in the Entity diagram I ended up with a ProductImage having ProductColorId instead of ProductColor_Id.
Not too sure what I did differently (maybe checked the "Add foreign key properties to ProductImage entity" in the diagram.
Re created database from diagram and re created model classes from database (to be used in another project). The ProductImage now looks like this:
public partial class ProductImage
{
public int Id { get; set; }
[DisplayName("File name"),
Required(),
StringLength(255)]
public string FileName { get; set; }
public bool Default { get; set; }
public int ProductColorId { get; set; }
public virtual ProductColor ProductColor { get; set; }
}
As far as I can see the only difference is ProductColerId, must be a convention in Entity to specify relations this way.
A tip when working in the diagram:
In a one to many first click the one side (in my case one ProductColor has many ProductImage so I right click on ProductColor). Then when choosing add new => association it automatically sets ProductColor (the clicked item) to one. Now setting the many relation on the right side will automatically check the "add foreign key" checkbox.
If I were to click on the many side first (right click on ProductImage). Then change the left side dropdown to many and right side select ProductColor with one for multiplicity then the "add foreign key" check box needs to be checked manually.

Establish Foreign Key Connection Using Entity Framework With SQL Queries

I have a couple of classes (for this example anyway) that use code first with the entity framework to connect to the database.
public class Customer
{
[Key]
public long CustomerId { get; set; }
public string CompanyName { get; set; }
...
public virtual List<Contact> Contacts { get; set; }
}
public class Contact
{
[Key]
public long ContactId { get; set; }
public string Forename { get; set; }
...
public long CustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
When I hook these up in my context class directly to the db the foreign key relationships hook up fine and I can access the collection of contacts from within the customer class.
class RemoteServerContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Contact> Contacts { get; set; }
...
}
My problem is that these database tables are used by various different systems and are massive. In order to increase efficiency I have overridden the default behaviour to point at a view (and also a stored proc elsewhere) rather than directly at the table.
public IEnumerable<Customer> Customers ()
{
return Database.SqlQuery<Customer>("SELECT * FROM vw_CustomerList");
}
public IEnumerable<Contact> Contacts()
{
return Database.SqlQuery<Contact>("SELECT * FROM vw_ContactsList");
}
I have made sure that in each of the views I have included the foreign key fields: CustomerId and ContactId.
When I do this however the class joins appear to be lost - there's always a null when I drill into either of the objects where it should be pointing to the other one. I have tried to set up what the foreign key field should point to but this doesn't seem to help either.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Contact>().HasRequired(p => p.Customer)
.WithMany()
.HasForeignKey(k => k.CustomerId);
}
Is there a way to establish the connection when overriding the default behaviour?
There is no overriding in this case. If you removed
public DbSet<Customer> Customers { get; set; }
and replaced it with
public IEnumerable<Customer> Customers ()
{
return Database.SqlQuery<Customer>("SELECT * FROM vw_CustomerList");
}
you have completely changed the behavior. The first uses entities and full power of EF. The second is only helper to execute custom SQL. Second without first or without defining entity in OnModelCreating doesn't use Customer as mapped entity at all - it uses it as any normal class (only mapped entities can use features like lazy loading).
Because your Customer is now mapped to view you cannot use your former Customer class used with table. You must define mapping of Customer to a view by cheating EF:
modelBuilder.Entity<Customer>().ToTable("vw_ContactsList"); // EF code fist has no view mapping
Once you have this you can try again using:
public DbSet<Customer> Customers { get; set; }
Unless your view is updatable you will get exception each time you try to add, update or delete any customer in this set. After mapping relation between Customer and Contact mapped to views your navigation properties should hopefully work.
The problem with SqlQuery is the way how it works. It returns detached entities. Detached entities are not connected to the context and they will not lazy load its navigation properties. You must manually attach each Customer instance back to context and to do that you again need DbSet.

Resources