實體框架7與其他領域的多對多關係

asp.net-core entity-framework entity-framework-core

我正在使用ASP.NET Core和Entity Framework 7.我有一個甲板類和一個卡類。我正在嘗試創建一個代碼,第一個DB具有多對多關係和一個額外的字段 - 甲板中每張卡的數量。這是我的代碼。

甲板課程:

public class Deck
{
    public int DeckId { get; set; }
    [Required]
    [MinLength(8)]
    public string Name { get; set; }
    public string Description { get; set; }
    public short Score { get; set; }
    [HasHeroValidate]
    public Hero Hero { get; set; }

    public short AspectOrder { get; set; }
    public short AspectWisdom { get; set; }
    public short AspectNature { get; set; }
    public short AspectRage { get; set; }
    public short AspectDominion { get; set; }
    public short AspectCorruption { get; set; }

    [CardCountValidate]
    public ICollection<DeckCard> DecksCards { get; set; }
    public ICollection<Comment> Comments { get; set; }
}

卡類:

public class Card
{
    public int CardId { get; set; }
    public String Name { get; set; }
    public String ImgUri { get; set; }
    public short ManaCost { get; set; }
    public CardType CardType { get; set; }

    public short AspectOrder { get; set; }
    public short AspectWisdom { get; set; }
    public short AspectNature { get; set; }
    public short AspectRage { get; set; }
    public short AspectDominion { get; set; }
    public short AspectCorruption { get; set; }

    public ICollection<DeckCard> DecksCards { get; set; }
}

DeckCard課程:

public class DeckCard
{
    public int DeckId { get; set; }
    public int CardId { get; set; }

    public Deck Deck { get; set; }
    public Card Card { get; set; }

    public short Count { get; set; }
}

DBcontext:

public sealed class SwDbContext :IdentityDbContext<SwdUser>
{
    private static bool _created = false;
    public SwDbContext()
    {
        if (!_created)
        {
            _created = true;
            Database.EnsureCreated();
        }
    }

    public DbSet<Deck> Decks { get; set; }
    public DbSet<Card> Cards { get; set; }
    public DbSet<Hero> Heroes { get; set; }
    public DbSet<Comment> Comments { get; set; }
    public DbSet<DeckCard> DeckCards { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<SwdUser>().HasMany(u => u.Comments).WithOne(c => c.User);
        modelBuilder.Entity<Deck>().HasMany(d => d.Comments).WithOne(c => c.Deck);
        modelBuilder.Entity<DeckCard>().HasKey(x => new {x.DeckId, x.CardId});
        base.OnModelCreating(modelBuilder);
    }
}

套餐:

dependencies": {
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.Framework.DependencyInjection": "1.0.0-beta8",
    "EntityFramework.Commands": "7.0.0-rc1-final",
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc1-final",
    "Microsoft.AspNet.Authentication.Cookies": "1.0.0-rc1-final",
    "Microsoft.AspNet.Authorization": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
    "Serilog.Framework.Logging": "1.0.0-rc1-final-10078",
    "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
    "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
    "EntityFramework.Core": "7.0.0-rc1-final",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final"

不幸的是,這會創建一個包含其他ID的表: DeckId,CardId,DeckDeckId,CardCardId

我也試過這裡建議的方法,但接下來

dnx ef遷移添加

命令給我一個錯誤,我應該使用流暢的api而不是屬性,甚至不能創建數據庫。 HasForeignKey()中沒有HasForeignKey()方法,所以我不能嘗試。

我做錯了還是在EF 7中還沒有實現?

熱門答案

這很奇怪,但似乎代碼沒有問題。由於一些文件所有權問題,我不得不重新安裝Windows,儘管DNVM,Visual Studio和ASP .NET版本完全相同,但現在可以按預期工作,無需更改代碼或包。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow