[英]Many-to-Many relation in EF Api Fluent error
當我嘗試與 Fluent API on.Net Core 建立多對多關系時,出現add-migration
錯誤:
The property or navigation 'MembreId' cannot be added to the entity type 'Gracci.Models.Achat' because a property or navigation with the same name already exists on entity type 'Gracci.Models.Achat'.
Model Achat
包含用戶的購買
public class Achat
{
public int Id { get; set; }
public int MembreId { get; set; }
public Membre Membre { get; set; }
public int ProduitId { get; set; }
public Produit Produit { get; set; }
public DateTime Date { get; set; }
public int Unite { get; set; }
}
Membre
會員包含會員資料
public class Membre
{
public int Id { get; set; }
[Required]
[MaxLength(35)]
public string Nom { get; set; }
[Required]
[MaxLength(35)]
public string Prenom { get; set; }
[Required]
[MaxLength(255)]
public string Email { get; set; }
[Required]
[MaxLength(255)]
public string MotDePasse { get; set; }
[Required]
[DefaultValue(false)]
public bool IsResponsable { get; set; }
[Required]
[DefaultValue(0.0)]
public float Solde { get; set; }
public ICollection<Achat> Achats { get; set; }
}
Produit
包含待售產品
public class Produit
{
public int Id { get; set; }
[Required]
[MaxLength(150)]
public string Name { get; set; }
[Required]
public float Price { get; set; }
[Required]
public int Stock { get; set; }
[Required]
[DefaultValue(true)]
public bool? isActif { get; set; }
public ICollection<Achat> Achats { get; set; }
}
我在GracciDbContext
中的Member
- Purchase
- Product
之間關系的代碼。 我確實添加DbSet<Membre>
、 DbSet<Achat>
和DbSet<Produit>
/* FLUENT API ACHAT */
modelBuilder.Entity<Achat>()
.Property(s => s.Unite)
.IsRequired()
.HasDefaultValue(1);
modelBuilder.Entity<Achat>()
.Property(s => s.Date)
.IsRequired()
.HasDefaultValueSql("getdate()");
/* FLUENT API ACHAT - PRODUIT/MEMBRE MANY-TO-MANY */
modelBuilder.Entity<Achat>().HasKey(p => new { p.MembreId, p.ProduitId});*/
modelBuilder.Entity<Achat>()
.HasOne<Produit>(achat => achat.Produit)
.WithMany(prod => prod.Achats)
.HasForeignKey(s => s.ProduitId);
modelBuilder.Entity<Achat>()
.HasOne<Membre>(achat => achat.Membre)
.WithMany(memb => memb.Achats)
.HasForeignKey(s => s.MembreId);
我曾使用 Fluent Api 進行約束,但我將其轉換為注釋......
我已刪除相應文件夾中存在的遷移。 並且添加遷移工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.