【问题标题】:Entity Framework 7 many to many relationship with additional fieldEntity Framework 7 与附加字段的多对多关系
【发布时间】:2016-03-23 23:44:21
【问题描述】:

我正在使用 ASP.NET Core 和 Entity Framework 7。我有一个卡片组和一个卡片类。我正在尝试创建一个具有多对多关系和一个附加字段的代码优先数据库 - 牌组中每张卡片的计数。这是我的代码。

甲板类:

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; }
}

数据库上下文:

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

我也尝试过here建议的方法,但是

dnx ef 迁移添加

命令给了我一个错误,我应该使用流利的 api 而不是属性,甚至无法创建数据库。 EF7 中没有HasForeignKey() 方法,所以我不能尝试。

我做错了什么还是 EF 7 中尚未实现?

【问题讨论】:

  • 什么意思? HasOne 在哪个属性上?
  • 你的场景有点类似于你已经配置好的 Deck User 关系。所以你应该能够使用几乎相同的模式来解决它(假设我没有错过任何东西):modelBuilder.Entity().HasMany(d => d.DeckCard).WithOne(c => c.甲板); modelBuilder.Entity().HasMany(d => d.DeckCard).WithOne(c => c.Card);

标签: entity-framework asp.net-core entity-framework-core


【解决方案1】:

这很奇怪,但代码似乎没有问题。由于一些文件所有权问题,我不得不重新安装 Windows,虽然 DNVM、Visual Studio 和 ASP .NET 版本完全相同,但现在可以正常工作,无需更改代码或包。

【讨论】:

    猜你喜欢
    • 2011-07-22
    • 2016-01-28
    • 2012-03-25
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 2013-11-18
    • 1970-01-01
    相关资源
    最近更新 更多