簡體   English   中英

流利的API的3對多對多

[英]3 way many to many with fluent api

我想先使用代碼和流暢的api創建具有3種方式關系的數據庫表。

在我想像的示例中,團隊應具有貓,狗和豬的獨特組合。 另一個團隊可以包含相同的貓和豬,但不能包含相同的狗,依此類推。

首先,我希望能夠容納包含特定動物的團隊。 myCat.Teams()如果可能的話,我也想強制唯一性。

public class Cat
{
    public int Id { get; set; }
    public virtual ICollection<Team> Teams { get; set; }
}
public class Dog
{
    public int Id { get; set; }
   public virtual ICollection<Team> Teams { get; set; }
}
public class Pig
{
    public Guid { get; set; }
   public virtual ICollection<Team> Teams { get; set; }
}

public class Team
{
    public int Id { get; set; }
    public int CatId { get; set; }
    public int DogId { get; set; }
    public Guid PigId { get; set; }

    public virtual Cat Cat {get; set;}
    public virtual Dog Dog {get; set;}
    public virtual Pig Pig {get; set;}
}

在OnModelCreating()中,為這些對象(CatMap,DogMap,PigMap,TeamMap)添加了EntityTypeConfigurations。

我嘗試從TeamMap類或其他方向設置HasMany關系。 例如,在DogMap中:

        HasMany(t => t.Teams)
        .WithRequired(t => t.Dog)
        .HasForeignKey(t => t.DogId);

但是每當我嘗試添加遷移時,都會出現類似以下錯誤:

tSystem.Data.Entity.Edm.EdmAssociationConstraint::關系約束中的從屬角色和主體角色的屬性數量必須相同。

如何正確設置這些關聯以實現上述兩個目標? 謝謝!!

團隊類不應具有自己的ID,因為主鍵是貓,狗,豬的組合。 因此,它應該類似於:

public class Team
{

    public int CatId { get; set; }

    public int DogId { get; set; }

    public Guid PigId { get; set; }

    public virtual Cat Cat { get; set; }
    public virtual Dog Dog { get; set; }
    public virtual Pig Pig { get; set; }
}

制圖:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    //primary key, composed by a combination
    modelBuilder.Entity<Team>()
        .HasKey(i => new { i.CatId, i.DogId, i.PigId });

    modelBuilder.Entity<Team>()
        .HasRequired(i => i.Cat)
        .WithMany(i => i.Teams)
        .HasForeignKey(i => i.CatId)
        .WillCascadeOnDelete(false);

    modelBuilder.Entity<Team>()
        .HasRequired(i => i.Dog)
        .WithMany(i => i.Teams)
        .HasForeignKey(i => i.DogId)
        .WillCascadeOnDelete(false);

    modelBuilder.Entity<Team>()
        .HasRequired(i => i.Pig)
        .WithMany(i => i.Teams)
        .HasForeignKey(i => i.PigId)
        .WillCascadeOnDelete(false);

    base.OnModelCreating(modelBuilder);
}

產生的遷移:

CreateTable(
    "dbo.Teams",
    c => new
        {
            CatId = c.Int(nullable: false),
            DogId = c.Int(nullable: false),
            PigId = c.Guid(nullable: false),
        })
    .PrimaryKey(t => new { t.CatId, t.DogId, t.PigId })
    .ForeignKey("dbo.Cats", t => t.CatId, cascadeDelete: true)
    .ForeignKey("dbo.Dogs", t => t.DogId, cascadeDelete: true)
    .ForeignKey("dbo.Pigs", t => t.PigId, cascadeDelete: true)
    .Index(t => t.CatId)
    .Index(t => t.DogId)
    .Index(t => t.PigId);

CreateTable(
    "dbo.Cats",
    c => new
        {
            Id = c.Int(nullable: false, identity: true),
        })
    .PrimaryKey(t => t.Id);

CreateTable(
    "dbo.Dogs",
    c => new
        {
            Id = c.Int(nullable: false, identity: true),
        })
    .PrimaryKey(t => t.Id);

CreateTable(
    "dbo.Pigs",
    c => new
        {
            PigId = c.Guid(nullable: false),
        })
    .PrimaryKey(t => t.PigId);

如果團隊出於某種原因必須擁有自己的ID; 更改模型,如下所示:

public class Team
{

    public int TeamId { get; set; }

    //....
}

制圖:

modelBuilder.Entity<Team>()
    .HasKey(i => i.TeamId);

//if you want to make the teamId an auto-generated column
modelBuilder.Entity<Team>()
     .Property(i => i.TeamId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

//if you want to make the cat, dog, and pig combination unique
modelBuilder.Entity<Team>()
    .Property(i => i.CatId)
    .HasColumnAnnotation(IndexAnnotation.AnnotationName,
    new IndexAnnotation(
        new IndexAttribute("IX_TeamComp", 1) { IsUnique = true }));

modelBuilder.Entity<Team>()
    .Property(i => i.DogId)
    .HasColumnAnnotation(IndexAnnotation.AnnotationName,
    new IndexAnnotation(
        new IndexAttribute("IX_TeamComp",2) { IsUnique = true }));

modelBuilder.Entity<Team>()
    .Property(i => i.PigId)
    .HasColumnAnnotation(IndexAnnotation.AnnotationName,
    new IndexAnnotation(
        new IndexAttribute("IX_TeamComp", 3) { IsUnique = true }));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM