简体   繁体   English

实体框架7关系问题

[英]Entity Framework 7 relationship issue

In EF 7 (beta 6) i'm using code-first and i made this code: 在EF 7(测试版6)中,我使用代码优先,并编写了以下代码:

public class User {
    // Primary key
    public int Id {get;set;}
    // Some attributes
    public string FirstName {get; set;} 
    public string LastName {get; set;}
    // Foreign keys
    public int CityOfBirthId {get;set;}
    public int CityHomeId {get;set;}
    // Navigation methods
    [ForeignKey("CityOfBirthId")]
    public virtual City CityOfBirth {get;set;}
    [ForeignKey("CityHomeId")]
    public virtual City CityHome {get;set;}
}

public class City {
    // Primary key
    public int Id {get;set;}
    // Attributes
    public string Name {get;set;}
    // Navigation methods
    [InverseProperty("CityOfBirth")]
    public virtual ICollection<User> UsersBorn {get;set;}
    [InverseProperty("CityHome")]
    public virtual ICollection<User> UsersHome {get;set;}
}

I also added this code in my ApplicationDBContext 我也在我的ApplicationDBContext中添加了此代码

public DbSet<User> Users {get;set;}
public DbSet<City> Cities {get;set;}

I've some problems with migration tool. 我在使用迁移工具时遇到了一些问题。 With just on relation per entity (for example i just write CityHome relation) it works, but with two relations (like the code above) the migration tool doesn't create relations. 每个实体只有一个关系(例如,我只是编写CityHome关系),它可以工作,但是有两个关系(如上面的代码),迁移工具不会创建关系。 In the tables i get only attributes and CityOfBirthId and CityHomeId are just int attributes istead of foreign keys. 在表中,我仅获得属性,而CityOfBirthId和CityHomeId只是int属性,而不是外键。

Upgrade to beta7. 升级到beta7。 Data attributes are not recognized in beta 6. Beta 6无法识别数据属性。

Ref https://github.com/aspnet/EntityFramework/pull/2756 参考https://github.com/aspnet/EntityFramework/pull/2756

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM