I have trouble understanding why a EF generated SELECT clause contains the primary key twice, the second one is postfixed with '1'.
exec sp_executesql N'SELECT [entity.WebAdminCompanyUser].[CompanyId], [entity.WebAdminCompanyUser].[AspNetUserId], [entity.WebAdminCompanyUser].[CompanyId1]
FROM [SafeProtect].[WebAdminCompanyUser] AS [entity.WebAdminCompanyUser]
INNER JOIN (
SELECT [entity1].[AspNetUserId]
FROM [SafeProtect].[WebAdminUser] AS [entity1]
WHERE ([entity1].[RowEnabled] = 1) AND EXISTS (
SELECT 1
FROM [SafeProtect].[WebAdminCompanyUser] AS [companyUser1]
WHERE ([companyUser1].[CompanyId] = #__companyId_0) AND ([entity1].[AspNetUserId] = [companyUser1].[AspNetUserId]))
) AS [t0] ON [entity.WebAdminCompanyUser].[AspNetUserId] = [t0].[AspNetUserId]
ORDER BY [t0].[AspNetUserId]',N'#__companyId_0 int',#__companyId_0=1
It fails with Invalid column name 'CompanyId1'.
Following are the entities and the corresponding configurations (fluent API):
WebAdminCompanyUser:
public partial class WebAdminCompanyUser : ITrackable, IMergeable
{
public WebAdminCompanyUser()
{
AdditionalInit();
}
public int CompanyId { get; set; }
public int AspNetUserId { get; set; }
public virtual Company Company { get; set; }
[NotMapped]
public TrackingState TrackingState { get; set; }
[NotMapped]
public ICollection<string> ModifiedProperties { get; set; }
[NotMapped]
public Guid EntityIdentifier { get; set; }
partial void AdditionalInit();
}
}
Configuration:
builder.Entity<WebAdminCompanyUser>(entity =>
{
entity.ToTable(name: "WebAdminCompanyUser", schema: SqlSchema.SafeProtect);
entity.HasKey("CompanyId", "AspNetUserId");
entity
.HasOne(d => d.Company)
.WithMany()
.HasForeignKey(d => d.CompanyId)
.IsRequired();
});
WebAdminUser:
public partial class WebAdminUser : IdentityUser<int>, IAuditInfo, IRowDisableableWithDateTime, ITrackable, IMergeable
{
public WebAdminUser()
{
WebAdminCompanyUser = new HashSet<WebAdminCompanyUser>();
WebAdminUserRole = new HashSet<WebAdminUserRole>();
WebAdminUserClaim = new HashSet<WebAdminUserClaim>();
WebAdminUserLogin = new HashSet<WebAdminUserLogin>();
AdditionalInit();
}
public string DisplayName { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime? ModifiedOn { get; set; }
public bool RowEnabled { get; set; }
public DateTime? DisabledOn { get; set; }
public virtual ICollection<WebAdminCompanyUser> WebAdminCompanyUser { get; set; }
public virtual ICollection<WebAdminUserRole> WebAdminUserRole { get; set; }
public virtual ICollection<WebAdminUserClaim> WebAdminUserClaim { get; set; }
public virtual ICollection<WebAdminUserLogin> WebAdminUserLogin { get; set; }
[NotMapped]
public TrackingState TrackingState { get; set; }
[NotMapped]
public ICollection<string> ModifiedProperties { get; set; }
[NotMapped]
public Guid EntityIdentifier { get; set; }
partial void AdditionalInit();
}
Configuration:
builder.Entity<WebAdminUser>(entity =>
{
entity.ToTable(name: "WebAdminUser", schema: SqlSchema.SafeProtect);
entity.Property(e => e.Id).HasColumnName("AspNetUserId");
// authorize multiple user name
entity.HasIndex((p) => new { p.UserName }).IsUnique(false);
entity
.HasMany(user => user.WebAdminUserClaim)
.WithOne()
.HasForeignKey(userClaims => userClaims.UserId)
.IsRequired();
entity
.HasMany(user => user.WebAdminUserLogin)
.WithOne()
.HasForeignKey(userLogin => userLogin.UserId)
.IsRequired();
entity
.HasMany(user => user.WebAdminUserRole)
.WithOne()
.HasForeignKey(userRole => userRole.UserId)
.IsRequired();
entity
.HasMany(user => user.WebAdminCompanyUser)
.WithOne()
.HasForeignKey(companyUser => companyUser.AspNetUserId)
.IsRequired();
});
EF query:
IQueryable<WebAdminUser> query =
from WebAdminUser user WebAdminUserRepository.All()
.Include(user => user.WebAdminUserRole)
.ThenInclude(userRole => userRole.AspNetRole)
.Include(user => user.WebAdminCompanyUser)
where user.WebAdminCompanyUser.Any(companyUser => companyUser.CompanyId == companyId)
select user;
return query.ToList();
Any help appreciated.
This usually happens when you have improperly mapped relationship by leaving some navigation property out of fluent configuration.
Remember that each navigation property (collection or reference) represents a relationship. If you fluently configure relationships and use HasOne / HasMany / WithOne / WithMany w/o passing the navigation property, you are telling EF that the relationship has no navigation property for the corresponding end. But if you actually do have navigation property, EF will map it to a separate relationship with default FK column name. If the default property/column name is already used, EF will append index to it until it gets unique.
In your case, the WebAdminUser class and configuration you've shown are irrelevant. The invalid column name CompanyId1 indicates that the problem is with Company class which you haven't shown, and the WithMany() call here
.HasOne(d => d.Company)
.WithMany() // <--
Most likely your Company class has collection navigation property to WebAdminCompanyUser, something like this (virtual and the name of the property doesn't matter):
public virtual ICollection<WebAdminCompanyUser> CompanyUsers { get; set; }
then you need to change the above .WithMany() call with something like
.WithMany(c => c.CompanyUsers)
and the problem will be solved.
Related
I have complicated relationships between these entities:
Country
Airport
Airline
Flight
Country has many Airlines and many Airports.
Airline has one Countries, the same about Airport.
Airline has many Flights,
Airport has many DepartureFlights and ArrivalFlights (both are Flight type).
Flight has one Airline and one DepartureAirport and one ArrivalAirport (both are Airport type).
Country can have no airlines and airports,
Airline can have no Flights,
Airport can have neither DepartureFlights nor ArrivalFlights.
What I am trying to do is when Country is deleted, then all related Airlines and Airports are deleted, also when Airline or DepartureAirport or ArrivalAirport are deleted, all related Flights are deleted also,
but when updating my db after the migration is created I'm getting an error:
Introducing FOREIGN KEY constraint "FK_Flights_Airports_DepartureAirport" on table "Flights" may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
How to implement this behavior and prevent an error?
Here are my models:
Country:
public class Country
{
public int Id { get; set; }
/* other properties */
public virtual ICollection<Airport>? Airports { get; set; }
public virtual ICollection<Airline>? Airlines { get; set; }
}
Airline:
public class Airline
{
public int Id { get; set; }
/* other properties */
public int CountryId { get; set; }
public virtual Country Country { get; set; }
public virtual ICollection<Flight>? Flights { get; set; }
}
Airport:
public class Airport
{
public int Id { get; set; }
public string Name { get; set; }
/* other properties */
public int CountryId { get; set; }
public virtual Country Country { get; set; }
public virtual ICollection<Flight>? DepartureFlights { get; set; }
public virtual ICollection<Flight>? ArrivalFlights { get; set; }
}
Flight:
public class Flight
{
public int Id { get; set; }
/* other properties */
public int AirlineId { get; set; }
public int DepartureAirportId { get; set; }
public int ArrivalAirportId { get; set; }
public virtual Airline Airline { get; set; }
public virtual Airport DepartureAirport { get; set; }
public virtual Airport ArrivalAirport { get; set; }
}
After all the DBContext file:
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<Airline> Airlines { get; set; }
public DbSet<Airport> Airports { get; set; }
public DbSet<Country> Countries { get; set; }
public DbSet<Flight> Flights { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Airline>(x =>
{
x.HasKey(a => a.Id);
x.HasOne(c => c.Country)
.WithMany(a => a.Airlines)
.HasForeignKey(a => a.CountryId)
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity<Airport>(x =>
{
x.HasKey(a => a.Id);
x.HasOne(c => c.Country)
.WithMany(a => a.Airports)
.HasForeignKey(a => a.CountryId)
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity<Country>(x =>
{
x.HasKey(c => c.Id);
});
modelBuilder.Entity<Flight>(x =>
{
x.HasKey(f => f.Id);
x.HasOne(a => a.Airline)
.WithMany(f => f.Flights)
.HasForeignKey(f => f.AirlineId)
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
x.HasOne(a => a.DepartureAirport)
.WithMany(f => f.DepartureFlights)
.HasForeignKey(f => f.DepartureAirportId)
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
x.HasOne(a => a.ArrivalAirport)
.WithMany(f => f.ArrivalFlights)
.HasForeignKey(f => f.ArrivalAirportId)
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
}
}
How to implement this behavior and prevent an error?
This is officially known issue. Therefore, We have two ways to handle this scenario thus the error:
1. Change one or more of the relationships to not cascade delete.
In this scenario, we could make the Country relationship with Airport, Flight and Airlines optional by giving it a nullable foreign key property: for instance we can do something like:
.IsRequired(false);
Note: You can check our official document for more details.
2. Configure the database without one or more of these cascade deletes,
then ensure all dependent entities are loaded so that EF Core can
perform the cascading behavior.
Considering this appreach we can keep the Airport, Flight and Airlines relationship required and configured for cascade delete, but make this configuration only apply to tracked entities, not the database: So we can do somethng like below:
modelBuilder.Entity<Airline>(x =>
{
x.HasKey(a => a.Id);
x.HasOne(c => c.Country)
.WithMany(a => a.Airlines)
.HasForeignKey(a => a.CountryId)
.OnDelete(DeleteBehavior.ClientCascade);
.IsRequired(false);
});
modelBuilder.Entity<Airport>(x =>
{
x.HasKey(a => a.Id);
x.HasOne(c => c.Country)
.WithMany(a => a.Airports)
.HasForeignKey(a => a.CountryId)
.OnDelete(DeleteBehavior.ClientCascade);
.IsRequired(false);
});
Note: You can apply same for Flight as well. In addition, As you may know OnDelete(DeleteBehavior.ClientCascade); or ClientCascade allows the DBContext to delete entities even if there is a cyclic ref or LOCK on it. Please read the official guideline for more details here
add these lines in "OnModelCreating"
var cascades = modelBuilder.Model.GetEntityTypes()
.SelectMany(t => t.GetForeignKeys())
.Where(fk => !fk.IsOwnership && fk.DeleteBehavior == DeleteBehavior.Cascade);
foreach (var fk in cascades)
fk.DeleteBehavior = DeleteBehavior.Restrict;
Am trying to make an update on the database with one-to-one relationship models, but only the Student model fields are the only ones working. How can I make the rest update too?
This is my Repository method for updating :
public void Edit(Student student)
{
var existingStudent = _context.Students
.Include(e => e.Education)
.Include(s => s.Siblings)
.Include(p => p.Parents)
.Include(g => g.Guardian)
.Single(s => s.Id == student.Id);
if (existingStudent != null)
{
// do some updating.
_context.Attach(existingStudent);
_context.Entry(existingStudent).CurrentValues.SetValues(student);
_context.Entry(existingStudent).State = EntityState.Modified;
_context.SaveChanges();
}
}
Then, this is my Student model class :
public class Student
{
[Key]
public long Id { get; set; }
[Display(Name="First Name")]
public string FirstName { get; set; }
[Display(Name="Middle Name")]
public string MiddleName { get; set; }
[Display(Name="Last Name")]
public string LastName { get; set; }
[Display(Name="Nationality")]
public string Nationality { get; set; }
[Display(Name="Gender")]
public string Gender { get; set; }
[Display(Name="Religion")]
public string Religion { get; set; }
[Display(Name="Medical Condition")]
public string MedicalCondition { get; set; }
[Display(Name="Deceased")]
public string Deceased { get; set; }
[Display(Name="Home Address")]
public string HomeAddress { get; set; }
[Display(Name="Country Of Residence")]
public string CountryOfResidence { get; set; }
[Display(Name="City")]
public string City { get; set; }
[Display(Name="Date Of Birth")]
public DateTime DateOfBirth { get; set; }
public int Age { get; set; }
public virtual Parents Parents { get; set; }
public virtual Education Education { get; set; }
public virtual Guardian Guardian { get; set; }
public virtual Siblings Siblings { get; set; }
}
Then one of the one-to-one relationship classes is as below :
public class Siblings
{
public long Id { get; set; }
[Display(Name="Number of Brothers")]
public int NumberOfBrothers { get; set; }
[Display(Name="Number of Sisters")]
public int NumberOfSisters { get; set; }
public Student Student { get; set; }
public long? StudentId { get; set; }
}
The rest of the related model classes Parents, Education, Guardian are the same as Siblings.
How can I be able to ensure that the update cuts across all the fields. Thanks.
EDIT
This is what I have in OnModelCreating() :
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Student>()
.HasOne(e => e.Education)
.WithOne(s => s.Student)
;
builder.Entity<Student>()
.HasOne(g => g.Guardian)
.WithOne(s => s.Student);
builder.Entity<Student>()
.HasOne(p => p.Parents)
.WithOne(s => s.Student);
builder.Entity<Student>()
.HasOne(s => s.Siblings)
.WithOne(s => s.Student);
builder.Entity<Education>()
.HasOne(s => s.Student)
.WithOne(e => e.Education);
builder.Entity<Guardian>()
.HasOne(s => s.Student)
.WithOne(g => g.Guardian);
builder.Entity<Parents>()
.HasOne(s => s.Student)
.WithOne(p => p.Parents);
builder.Entity<Siblings>()
.HasOne(s => s.Student)
.WithOne(p => p.Siblings);
}
I suggest you try two ways below:
Modify the navigation property
_context.Entry(existingStudent).CurrentValues.SetValues(student);
_context.Entry(existingStudent).State = EntityState.Modified;
_context.Entry(existingStudent.Siblings).CurrentValues.SetValues(student.Siblings);
_context.Entry(existingStudent.Siblings).State = EntityState.Modified;
Update with _context.Update
var existingStudent = _context.Student
.Include(s => s.Siblings)
.AsNoTracking()
.Single(s => s.Id == 4);
if (existingStudent != null)
{
existingStudent = student;
_context.Update(existingStudent);
_context.SaveChanges();
}
I'm not sure if you have any Fluent configuration, but if you don't,
As seen in this link
You need to configure the relationship using the Fluent API.
Without knowing your complete requirement, I'm going to take a stab in the dark that this is probably what you're looking for.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>()
.HasOptional(s => s.Siblings) // Mark Siblings property optional in Student entity
.WithRequired(ad => ad.Student); // mark Student property as required in Siblings entity. Cannot save Siblings without Student
}
EDIT
If HasOptional and WithRequired are giving you red squiggles, then you're most likely using EF CORE and not EF 6, unless you have a major problem elsewhere.
Addressing your current problem, in your Fluent configuration (OnModelCreating())
First, what you are looking for is not how to configure one-to-one relationships. Your intending to configure one-to-zero or one relationships.
Second, given the following configuration
builder.Entity<Student>()
.HasOne(s => s.Siblings)
.WithOne(s => s.Student);
This means: Entity<Student> requires one Sibling, and Entity<Sibling> requires one Student So your configuration is not doing what you want it to do.
Last, since EF Core has some pretty smart conventions, it will actually figure out that your nullable foreign key means it's optional, and will create your (0->0..1) relationship for you with out any config.
The Solution, simply remove all fluent config from your OnModelCreating() and let EF do it's job. And don't forget to update-database.
I do not know if this behavior is by design or it is a bug in EF6, or there is another way to do this. Having this complex type:
[ComplexType]
public partial class Company
public bool HasValue { get { return !string.IsNullOrEmpty(this.Name); } }
[MaxLength(100)]
public string Name { get; set; }
[MaxLength(20)]
public string PhoneNumber { get; set; }
[MaxLength(128)]
public string EmailAddress { get; set; }
}
I reuse it in these two entities:
public partial class Customer
{
public Customer ()
{
this.Company = new Company();
}
[Key]
public int IdCustomer { get; set; }
[MaxLength(100)]
[Required]
public string FirstName { get; set; }
[MaxLength(100)]
[Required]
public string LastName { get; set; }
public Company Company { get; set; }
public virtual AcademicInfo AcademicInfo { get; set; }
}
public partial class AcademicInfo
{
public AcademicInfo()
{
this.Organization = new Company();
}
[Key, ForeignKey("Customer")]
public int IdCustomer { get; set; }
public Company Organization { get; set; }
[MaxLength(100)]
public string Subject { get; set; }
[MaxLength(100)]
public string Degree { get; set; }
public virtual Customer Customer { get; set; }
}
in the dbcontext's OnModelCreating (EDIT: I added FK code I omitted before for simplicity):
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
// ... Other code here related to entities not related to the problem reported omitted to avoid confusion.
modelBuilder.Entity<AcademicInfo>()
.HasRequired(a => a.Customer)
.WithOptional(c => c.AcademicInfo)
.WillCascadeOnDelete(true);
modelBuilder.Entity<Customer>()
.Property(p => p.Company.Name)
.HasColumnName("CompanyName")
.IsOptional(); // CONFLICT HERE
modelBuilder.Entity<Customer>()
.Property(p => p.Company.EmailAddress)
.HasColumnName("CompanyEmailAddress")
.IsOptional(); //CONFLICT HERE
modelBuilder.Entity<Customer>()
.Property(p => p.Company.PhoneNumber)
.HasColumnName("CompanyPhoneNumber")
.IsOptional();
modelBuilder.Entity<AcademicInfo>()
.Property(a => a.Organization.Name)
.HasColumnName("OrganizationName")
.IsRequired(); // CONFLICT
modelBuilder.Entity<AcademicInfo>()
.Property(a => a.Organization.EmailAddress)
.HasColumnName("OrganizationEmail")
.IsRequired(); // CONFLICT
modelBuilder.Entity<AcademicInfo>()
.Property(a => a.Organization.PhoneNumber)
.HasColumnName("OrganizationPhone")
.IsOptional();
}
The Add-Migration command fails with the following error:
Conflicting configuration settings were specified for property 'Name' on type 'Company':
IsNullable = False conflicts with IsNullable = True
But it has no sense because I defined the fields not nullable in AcademicInfo table and nullable in the Customer table.
This is an old question but still valid for EF version 6.1.3.
According to this issue the behavior is an Entity Framework limitation about how to configure a particular complex type.
This is a limitation of EF, some property facets need to be stored in C-Space and EF doesn't have a way of configuring a particular usage of a complex type. So you can only specify different S-Space facets like ColumnName or ColumnType
I'm trying to create a commenting system backed by Entity Framework Core where multiple entities of different type can have comments attached to them.
These are my entities. (In the real application there are about 7 in total with varying relationships but this is how it generally looks)
public class Comment : IEntityBase
{
public int Id { get; set; }
public int? FreezerId{ get; set; }
public Freezer Freezer { get; set; }
public int? BoxId{ get; set; }
public Box Box{ get; set; }
public string Author { get; set; }
public DateTime CreatedAt { get; set; }
public string Content { get; set; }
}
public class Freezer: IEntityBase
{
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public ICollection<Box> Boxes{ get; set; }
public ICollection<Comment> Comments { get; set; }
}
public class Box: IEntityBase
{
public int Id { get; set; }
public Freezer Freezer{get; set;}
public int FreezerId{get; set;}
public string Data{ get; set; }
public ICollection<Comment> Comments { get; set; }
}
I want the Comment entity to be attached to one Freezer or one Box, but not both at the same time.
I defined the relationship in the fluent API as the following:
builder.Entity<Box>(boxBuilder=>
{
boxBuilder.HasOne(box=> box.Freezer)
.WithMany(freezer => freezer.boxes)
.HasForeignKey(box => box.FreezerId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
boxBuilder.HasMany(box => box.Comments)
.WithOne(comment => comment.Box)
.HasForeignKey(comment => comment.BoxId)
.OnDelete(DeleteBehavior.Cascade);
});
builder.Entity<Freezer>(freezerBuilder =>
{
freezerBuilder.HasMany(freezer=> freezer.Comments)
.WithOne(comment => comment.Freezer)
.HasForeignKey(comment => comment.FreezerId)
.OnDelete(DeleteBehavior.Cascade);
});
When I try to update the database to this model I get the following error:
System.Data.SqlClient.SqlException: Introducing FOREIGN KEY constraint 'FK_Comment_Boxes_BoxId' on table 'Comment' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
I think the error comes from the Box and the Freezer property in the Comment class not being optional which would make this a 1 to many relationship instead of a 0..1 to many relationship which is what I want.
With Entity Framework 6 I would just use the .HasOptional() method, but this doesn't exist in Entity Framework Core
I think one way to solve this would be to just subclass the Comment class and create a unique comment class for each entity that can be commented on and move the foreign key and reference property to that subclass instead.
But it feels like I shouldn't have to do it this way.
You have to disable the cascade delete(DeleteBehavior.Restrict) then it will works for you:
modelBuilder.Entity<Box>(boxBuilder =>
{
boxBuilder.HasOne(box => box.Freezer)
.WithMany(freezer => freezer.Boxes)
.HasForeignKey(box => box.FreezerId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
boxBuilder.HasMany(box => box.Comments)
.WithOne(comment => comment.Box)
.HasForeignKey(comment => comment.BoxId)
.OnDelete(DeleteBehavior.Restrict);
});
modelBuilder.Entity<Freezer>(freezerBuilder =>
{
freezerBuilder.HasMany(freezer => freezer.Comments)
.WithOne(comment => comment.Freezer)
.HasForeignKey(comment => comment.FreezerId)
.OnDelete(DeleteBehavior.Restrict);
});
base.OnModelCreating(modelBuilder);
}
Usage:
using (var myConext = new MyDbContext())
{
myConext.Database.EnsureCreated();
myConext.Boxes.Add(new Box() {Freezer = new Freezer()});
myConext.SaveChanges();
}
I have a many to many relation, and I want to add an intermediate class, which would enable me adding the many to many relations using repository pattern.
What I can't figure out is the mapping.
Here's the structure
public class Product
{
public Product()
{
Categories = new HashSet<Category>();
}
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Category> Categories { get; set; }
}
public class Category
{
public int Id { get; set; }
public String Name { get; set; }
public ICollection<Product> Products { get; set; }
}
public class PCMap
{
public int product_id { get; set; }
public int category_id { get; set; }
public Product Product { get; set; }
public Category Category { get; set; }
}
And the Mapping
modelBuilder.Entity<Product>()
.HasEntitySetName("PCMap")
.HasMany(p=>p.Categories)
.WithMany(p=>p.Products)
.Map(m=>
{
m.MapLeftKey("product_id");
m.MapRightKey("category_id");
m.ToTable("PCMap");
});
modelBuilder.Entity<PCMap>()
.ToTable("PCMap");
modelBuilder.Entity<PCMap>().HasKey(k => new
{
k.category_id,
k.product_id
});
modelBuilder.Entity<PCMap>()
.HasRequired(p=>p.Product)
.WithMany()
.HasForeignKey(p => p.product_id);
modelBuilder.Entity<PCMap>()
.HasRequired(p => p.Category)
.WithMany()
.HasForeignKey(p => p.category_id);
Here's the error that I get..
How do I fix this ?
The way you've set it up. PCMap is a non entity and is just used to facilitate the M:N join under the hood.
Product p = new Product();
p.Categories ...
Category c = new Category();
c.Products ...
So beacuse you've already defined PC as part of the Product Entity definition here.
.Map(m=>
{
m.MapLeftKey("product_id");
m.MapRightKey("category_id");
m.ToTable("PCMap");
});
I don't believe you need to (or it's possible to) define it again, separately below. Try deleting all this code.
modelBuilder.Entity<PCMap>()
.ToTable("PCMap");
modelBuilder.Entity<PCMap>().HasKey(k => new
{
k.category_id,
k.product_id
});
modelBuilder.Entity<PCMap>()
.HasRequired(p=>p.Product)
.WithMany()
.HasForeignKey(p => p.product_id);
modelBuilder.Entity<PCMap>()
.HasRequired(p => p.Category)
.WithMany()
.HasForeignKey(p => p.category_id);