实体框架6代码第一 - 通过注释多对多的一种方式 [英] Entity framework 6 code first - one way many to many via annotations

查看:100
本文介绍了实体框架6代码第一 - 通过注释多对多的一种方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在实体框架6中使用代码第一和注释来创建单向多对多关联?示例:

Is it possible to create one-way many-to-many association in entity framework 6 with code first and annotations? Example:

class Currency
{
    public int id { get; set; }
}

class Country
{
    public int id { get; set; }

    // How i can annotate this property to say EF that it is many-to-many
    // and it should create mapping table?
    // I don't need navigation property to Country in Currency class!
    public virtual IList<Currency> currencies { get; set; }
}

在Java + JPA注释上,我可以实现我需要这样的方式: / p>

On Java + JPA annotations i can implement what i need this way:

@OneToMany
@JoinTable(name = "MAPPING_TABLE", joinColumns = {
    @JoinColumn(name = "THIS_ID", referencedColumnName = "ID")
}, inverseJoinColumns = {
    @JoinColumn(name = "OTHER_ID", referencedColumnName = "ID")
})

所以,EF是否具有相同的功能?

so, does EF have equal features?

推荐答案

您可以通过使用Fluent API明确指定关系来执行此操作。覆盖 DbContext 类的 OnModelCreating()方法,并在覆盖中指定映射表的详细信息,如此:

You can do this by specifying the relationship explicitly using the Fluent API. Override the OnModelCreating() method of your DbContext class, and in your override specify the details of the mapping table like this:

class MyContext : DbContext
{
    public DbSet<Currency> Currencies { get; set; }
    public DbSet<Country> Countries { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Country>()
            .HasMany(c => c.Currencies)
            .WithMany()                 // Note the empty WithMany()
            .Map(x =>
            {
                x.MapLeftKey("CountryId");
                x.MapRightKey("CurrencyId");
                x.ToTable("CountryCurrencyMapping");
            });

        base.OnModelCreating(modelBuilder);
    }
}

注意 - 在我的快速测试中 - 你会加载EF对象以填充列表时,必须 Include()货币属性:

Note that - in my quick test anyway - you will have to Include() the Currencies property when loading the EF object to have the list populated:

            var us = db.Countries
                        .Where(x => x.Name == "United States")
                        .Include(x=>x.Currencies)
                        .First();

编辑

如果你真的想使用数据注释完成所有操作,并且根本不使用Fluent,那么您可以根据其他位置明确建模连接表。尽管如此,这种方法有多种可用性的缺点,所以似乎Fluent方法是最好的方法。

If you really want to do everything with Data Annotations, and not use Fluent at all, then you can model the join table explicitly as pointed out elsewhere. There are various usability disadvantages of this approach, though, so it seems the Fluent method is the best approach.

class Country
{
    public int Id { get; set; }
    public virtual ICollection<CountryCurrency> CountryCurrencies { get; set; }
}

class Currency
{
    public int Id { get; set; }
}

class CountryCurrency
{
    [Key, Column(Order=0)]
    public virtual int CountryId { get; set; }
    [Key, Column(Order=1)]
    public virtual int CurrencyId { get; set; }

    public virtual Country Country { get; set; }
    public virtual Currency Currency { get; set; }
}

这篇关于实体框架6代码第一 - 通过注释多对多的一种方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆