EF6 Code first Composite Foreign key with Fluent API - entity-framework-6

I am having trouble creating One-one(zero) Foreign key relationship on code first EF6 fluent API
My Entities are
public class Invoice
{
public int AccountID { get; set; }
public int InvoiceID { get; set; }
public decimal Amount { get; set; }
public Refund Refund { get; set; }
internal static void ConfigureModel(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Invoice>()
.HasKey(e => new { e.AccountID, e.InvoiceID });
}
}
public class Refund
{
public int AccountID { get; set; }
public int RefundID { get; set; }
public decimal Amount { get; set; }
public int InvoiceID { get; set; }
public Invoice Invoice { get; set; }
internal static void ConfigureModel(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Refund>()
.HasKey(e => new { e.AccountID, e.RefundID });
modelBuilder.Entity<Refund>()
.HasRequired(e => e.Invoice )
// .HasForeignKey(e => new { e.AccountID, e.InvoiceID});
}
}
I can't define the composite foreign key on Refund table using fluent API
If I change the
public Refund Refund { get; set; }
to
public ICollection<Refund> Refund{ get; set; }
Then I can add this relation as
modelBuilder.Entity<Refund>()
.HasRequired(e => e.Invoice)
.WithMany(e => e.Refund)
.HasForeignKey(e => new { e.AccountID, e.InvoiceID});

Related

EF6 migration - FK to entity with composite Key error

I have the following entities:
public class MeetingAttendee
{
public int MeetingId { get; set; }
public Meeting Meeting { get; set; }
public int UserId { get; set; }
public User User { get; set; }
public IEnumerable<MeetingAttendanceEntry> MeetingAttendanceEntries { get; set; }
}
public class MeetingAttendanceEntry
{
public int Id { get; set; }
public int MeetingId { get; set; }
public Meeting Meeting { get; set; }
public int MeetingAttendeeId { get; set; }
public MeetingAttendee MeetingAttendee { get; set; }
public AttendanceStatus AttendanceStatus { get; set; }
}
The MeetingAttendee is set as:
modelBuilder.Entity<MeetingAttendee>().HasKey(pk => new { pk.MeetingId, pk.UserId });
modelBuilder.Entity<MeetingAttendee>().HasRequired(o => o.User).WithMany(m => m.MeetingAttendees).HasForeignKey(fk => fk.UserId);
modelBuilder.Entity<MeetingAttendee>().HasRequired(o => o.Meeting).WithMany(m => m.Attendees).HasForeignKey(fk => fk.MeetingId);
And when I'm trying to add MeetingAttendanceEntry, I'm getting "The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical" validation error.
I think is coming from the composite key, but everything I tried fails.
I tried from the parent entity:
modelBuilder.Entity<MeetingAttendee>().HasMany(m => m.MeetingAttendanceEntries).WithRequired(e => e.MeetingAttendee).HasForeignKey(e => e.MeetingAttendeeId);
From the child:
modelBuilder.Entity<MeetingAttendanceEntry>().HasRequired(e => e.MeetingAttendee).WithMany(m => m.MeetingAttendanceEntries).HasForeignKey(e => e.MeetingAttendeeId);
I tried adding the User and UserId to MeetingAttendanceEntry, and then setting it as FK as well:
modelBuilder.Entity<MeetingAttendanceEntry>().HasRequired(e => e.User).WithMany().HasForeignKey(e => e.UserId);
but still no luck.
What I'm missing?

How to relate model property to same model in ASP.NET MVC?

Sometimes, you want to store who registered or created a user account. It's either the user registered himself/herself or some other user account registered him, such as Admin accounts. So, the User table would something like:
public class User : Identity
{
public int Id { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string Name { get; set; }
// this is the part that I'd to relate to the same model
[ForeignKey("Id")]
public virtual User RegisteredBy { get; set; }
}
Using data annotations or Fluent API, how would you relate the User.RegisteredBy to User.Id?
Thanks in advance!
Something like in your class
public class User : Identity
{
public int Id { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string Name { get; set; }
// this is the self referential part
public int? RegisteredById { get; set; }
public virtual User RegisteredBy { get; set; }
public virtual ICollection<User> RegisteredUsers { get; set; }
}
and then in your DbContext
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasOptional(x => x.RegisteredBy)
.WithMany(x => x.RegisteredUsers)
.HasForeignKey(x => x.RegisteredById);
}
This is untested, but I did something similar in a project a little while ago and it worked fine.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasOptional(c => c.RegisteredBy)
.WithMany()
.HasForeignKey(c => c.RegisteredById);
}
You can use the above Fluent Api code and your class should look like this
public class User : Identity
{
public int Id { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string Name { get; set; }
public int? RegisteredById { get; set; }
public User RegisteredBy { get; set; }
}

Fluent API One to Many mapping table

It's really helpful if anyone could explain me how to create a mapping (associated)table for one to many relationship using Fluent API.`
public class Category
{
public int ID { get; set; }
public string Name { get; set; }
public List<Image> Images { get; set; }
}
public class Image
{
public int ID { get; set; }
public string Name { get; set; }
public List<Category> Category{ get; set; }
The mapping table should contain CategoryID and ImageID.
The solution should be something similar to this. (This is for many to many)
modelBuilder.Entity<Category>()
.HasMany(c => c.Images).WithMany(i => i.Categories)
.Map(t => t.MapLeftKey("CategoryID")
.MapRightKey("ImageID")
.ToTable("CategoryImage"));
I want Fluent API to create new mapping table for the below relationship.
public class Category
{
public List<Image> Images{get; set;}
}
public class Image
{
public Category Category{ get; set; }
}
Adding NewTable:
modelBuilder
.Entity<NewTable>()
.HasRequired(_ => _.Category)
.WithMany()
.HasForeignKey(_ => _.CategoryId);
modelBuilder
.Entity<Image>()
.HasRequired(_ => _.NewTable)
.WithMany(_ => _.Images)
.HasForeignKey(_ => _.NewTableId)
public class NewTable
{
public int ID { get; set; }
public int CategoryId { get; set; }
public List<Image> Images { get; set; }
public virtual Category Category { get; set; }
}
public class Image
{
public int ID { get; set; }
public string Name { get; set; }
public List<Category> Categories { get; set; }
public int NewTableId { get; set; }
public virtual NewTable NewTable { get; set; }
}

Table only with foreign key in entity framework 6 (Fluent Api)

How to map foreign keys from two different table to one table in fluent Api?
My two model is like
public class Customer
{
[Key]
public string Userid { get; set; }
public string PassWord { get; set; }
public bool premium { get; set; }
}
public class Roles
{
[Key]
public string Name { get; set; }
public string Description { get; set; }
}
And 3rd table which has primary key of above table as foreign key?
public class CustomerRoles
{
public string RoleName { get; set; }
public string UserId { get; set; }
}
How to map in Fluent Api?
public class Customer
{
[Key]
public string Userid { get; set; }
public string PassWord { get; set; }
public bool premium { get; set; }
public ICollection<CustomerRole> CustomerRoles { get; set; }
}
public class Role
{
[Key]
public string Name { get; set; }
public string Description { get; set; }
public ICollection<CustomerRole> CustomerRoles { get; set; }
}
public class CustomerRole
{
public string RoleName { get; set; }
public string UserId { get; set; }
public Role Role { get; set; }
public Customer Customer { get; set; }
}
public class AppContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Role> Roles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Customer>().HasMany(c => c.CustomerRoles).WithRequired(cr => cr.Customer);
modelBuilder.Entity<Role>().HasMany(r => r.CustomerRoles).WithRequired(cr => cr.Role);
modelBuilder.Entity<CustomerRole>().HasKey(cr => new { cr.RoleName, cr.UserId });
}
}
PS: Class name should not be plural, it can confuse with array property.
update how to use it
static void Main(string[] args)
{
using (var ctx = new AppContext())
{
Customer customer = new Customer { Userid = "A" };
ctx.Customers.Add(customer);
Role role1 = new Role { Name = "Role1" };
ctx.Roles.Add(role1);
Role role2 = new Role { Name = "Role2" };
ctx.Roles.Add(role2);
customer.CustomerRoles = new[]
{
new CustomerRole { Customer = customer, Role = role1 },
new CustomerRole { Customer = customer, Role = role2 },
};
ctx.SaveChanges();
}
}

Entity Framework Fluent API

My Entities are as follows...
public class Project{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Survey> Surveys { get; set; }
}
public class Survey{
public int Id { get; set; }
public int ProjectId { get; set; }
public string Name { get; set; }
public virtual Project Project { get; set; }
}
public class Category{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Survey> Surveys { get; set; }
}
public class SurveyCategory{
public int Id { get; set; }
public int SurveyId{ get; set; }
public int CategoryId { get; set; }
public string Name { get; set; }
public virtual Survey Survey { get; set; }
public virtual Category Category { get; set; }
}
A project will have list of Surveys, A Survey will have only one Category, A Category can have multiple Survey, SurveyCategory is the table where I am storing Survey + Category link.
Can anyone direct me to what would be appropriate Fluent API code will be for this to map properly.... So far I have this....
protected override void OnModelCreating(DbModelBuilder modelBuilder){
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Project>().HasMany(project => project.Surveys);}
hi I hope I understood what you are looking for.
First of al you should do a few changes to your model
public class Project
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Survey> Surveys { get; set; }
}
public class Survey
{
public int Id { get; set; }
public int ProjectId { get; set; }
public int CategoryId { get; set; }
public string Name { get; set; }
public virtual Project Project { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<SurveyCategory> SurveyCategory { get; set; }
}
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Survey> Surveys { get; set; }
public virtual ICollection<SurveyCategory> SurveyCategory { get; set; }
}
public class SurveyCategory
{
public int Id { get; set; }
public string Name { get; set; }
public int SurveyId { get; set; }
public virtual Survey Survey { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
}
And then on the model creating you should do this
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Project>()
.HasMany(p => p.Surveys)
.WithRequired(u => u.Project);
modelBuilder.Entity<Category>()
.HasMany(p => p.Surveys)
.WithRequired(u => u.Category);
modelBuilder.Entity<Category>()
.HasMany(p => p.SurveyCategory)
.WithRequired(u => u.Category)
.HasForeignKey(k => k.CategoryId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Survey>()
.HasMany(p => p.SurveyCategory)
.WithRequired(u => u.Survey)
.HasForeignKey(k => k.SurveyId)
.WillCascadeOnDelete(false);
}
I hope this helps

Resources