C# EF Api Fluent错误中的多对多关系
当我试图与.Net Core上的Fluent API建立多对多关系时,我遇到了C# EF Api Fluent错误中的多对多关系,c#,entity-framework,C#,Entity Framework,当我试图与.Net Core上的Fluent API建立多对多关系时,我遇到了添加迁移的错误: 无法将属性或导航“MembreId”添加到实体类型“Gracci.Models.Achat”,因为实体类型“Gracci.Models.Achat”上已存在同名的属性或导航。 型号Achat包含用户的购买 公共类Achat { 公共int Id{get;set;} public int MembreId{get;set;} 公共Membre Membre{get;set;} public int pr
添加迁移的错误:
无法将属性或导航“MembreId”添加到实体类型“Gracci.Models.Achat”,因为实体类型“Gracci.Models.Achat”上已存在同名的属性或导航。
型号Achat
包含用户的购买
公共类Achat
{
公共int Id{get;set;}
public int MembreId{get;set;}
公共Membre Membre{get;set;}
public int produtid{get;set;}
公共Produit Produit{get;set;}
公共日期时间日期{get;set;}
公共整数单位{get;set;}
}
模型Membre
包含成员数据
公共类成员
{
公共int Id{get;set;}
[必需]
[MaxLength(35)]
公共字符串Nom{get;set;}
[必需]
[MaxLength(35)]
公共字符串Prenom{get;set;}
[必需]
[最大长度(255)]
公共字符串电子邮件{get;set;}
[必需]
[最大长度(255)]
公共字符串MotDePasse{get;set;}
[必需]
[默认值(false)]
公共bool是可响应的{get;set;}
[必需]
[默认值(0.0)]
公共浮点Solde{get;set;}
公共ICollection Achats{get;set;}
}
型号Produit
包含待售产品
公共类产品
{
公共int Id{get;set;}
[必需]
[MaxLength(150)]
公共字符串名称{get;set;}
[必需]
公开浮动价格{get;set;}
[必需]
public int Stock{get;set;}
[必需]
[默认值(真)]
公共bool?isActif{get;set;}
公共ICollection Achats{get;set;}
}
在GracciDbContext
中,我的成员
-购买
-产品
之间关系的代码。我添加了DbSet
,DbSet
和DbSet
/*FLUENT API ACHAT*/
modelBuilder.Entity()
.Property(s=>s.Unite)
.IsRequired()
.HasDefaultValue(1);
modelBuilder.Entity()
.Property(s=>s.Date)
.IsRequired()
.HasDefaultValueSql(“getdate()”);
/*FLUENT API ACHAT-PRODUIT/MEMBRE多对多*/
modelBuilder.Entity().HasKey(p=>new{p.MembreId,p.ProduitId})*/
modelBuilder.Entity()
.HasOne(achat=>achat.Produit)
.WithMany(prod=>prod.achat)
.HasForeignKey(s=>s.produtid);
modelBuilder.Entity()
.HasOne(achat=>achat.Membre)
.WithMany(memb=>memb.achat)
.HasForeignKey(s=>s.MembreId);
我使用Fluent Api进行约束,但我将其转换为注释…我已删除相应文件夹中的迁移。添加迁移成功。我可以使用您的模型进行迁移。您是否使用了ef core?@user3104267 Nuget软件包Microsoft.AspNetCore.Identity.EntityFrameworkCore
,Microsoft.EntityFrameworkCore
,Microsoft.EntityFrameworkCore.SqlServer
和Microsoft.EntityFrameworkCore.Tools
。在我的代码中,我使用的是和Microsoft.EntityFrameworkCore