2015-11-17 100 views
2

我試圖運行EF代碼優先使用屬性/註釋方法與接口作爲屬性類型的遷移。我們已經用接口構建了一個完整的基礎架構,並且正在使用這些架構來實現具體的類並希望啓用遷移。 EF似乎沒有正確地關聯外鍵關係。有什麼方法可以糾正這個問題嗎?實體框架代碼與接口的第一次遷移

下面是一個例子:

我有一個IStateProvince界面如下:

public interface IStateProvince 
{ 

    string Abbreviation { get; set; } 

    string Name { get; set; } 

    ICountry Country { get; set; } 

} 

我也有一個ICountry界面如下:

public interface ICountry 
{ 

    string Name { get; set; } 

    [MaxLength(2)] 
    string TwoLetterIsoCode { get; set; } 

    [MaxLength(3)] 
    string ThreeLetterIsoCode { get; set; } 

    int NumericIsoCode { get; set; } 

    List<IStateProvince> StateProvinces { get; set; } 
} 

我已經創建具體實現如如下:

[Table("TypeData.Country")] 
public class Country : BaseSqlEntity, ICountry 
{ 

    [Required, MaxLength(250)] 
    public string Name { get; set; } 

    [Required] 
    public string TwoLetterIsoCode { get; set; } 

    [Required] 
    public string ThreeLetterIsoCode { get; set; } 

    public int NumericIsoCode { get; set; } 

    public virtual List<IStateProvince> StateProvinces { get; set; } 

} 

國家省如下:

[Table("TypeData.StateProvince")] 
public class StateProvince : BaseSqlEntity, IStateProvince 
{ 

    [Required, MaxLength(3)] 
    public string Abbreviation { get; set; } 

    [Required, MaxLength(250)] 
    public string Name { get; set; } 

    public Guid CountryId { get; set; } 

    [ForeignKey("CountryId")] 
    public virtual ICountry Country { get; set; } 

} 

您所看到的BaseSqlEntity如下:

public class BaseSqlEntity 
{ 
    [Key] 
    public Guid Id { get; set; } 

    public DateTime DateCreatedUtc { get; set; } 

    public DateTime DateModifiedUtc { get; set; } 

    public BaseSqlEntity() 
    { 
     DateCreatedUtc = DateModifiedUtc = DateTime.UtcNow; 
     Id = Guid.NewGuid(); 
    } 

} 

我已經添加在上下文中dbsets如下:

public DbSet<Country> Countries { get; set; } 
public DbSet<StateProvince> StateProvinces { get; set; } 

在運行遷移,我得到以下內容:

public override void Up() 
    { 
     CreateTable(
      "TypeData.Country", 
      c => new 
       { 
        Id = c.Guid(nullable: false), 
        Name = c.String(nullable: false, maxLength: 250), 
        TwoLetterIsoCode = c.String(nullable: false), 
        ThreeLetterIsoCode = c.String(nullable: false), 
        NumericIsoCode = c.Int(nullable: false), 
        DateCreatedUtc = c.DateTime(nullable: false), 
        DateModifiedUtc = c.DateTime(nullable: false), 
       }) 
      .PrimaryKey(t => t.Id); 

     CreateTable(
      "TypeData.StateProvince", 
      c => new 
       { 
        Id = c.Guid(nullable: false), 
        Abbreviation = c.String(nullable: false, maxLength: 3), 
        Name = c.String(nullable: false, maxLength: 250), 
        CountryId = c.Guid(nullable: false), 
        DateCreatedUtc = c.DateTime(nullable: false), 
        DateModifiedUtc = c.DateTime(nullable: false), 
       }) 
      .PrimaryKey(t => t.Id); 

    } 

正如您將會注意到的那樣,沒有外鍵關係生成,因爲它不能以某種方式將ICountry與其實施國相關聯。

如果我將ICountry的類型更改爲Country並對界面進行評論,它就可以正常生成關係。

有沒有辦法解決這個問題?任何幫助將非常感激。我們已經使用我們喜歡使用的接口構建了大量可重用的架構,但看起來這可能是一個阻礙。

+0

嘗試在運行遷移後手動將外鍵添加到由遷移生成的代碼。 – alikuli

回答

4

您仍然可以使用這些接口。
只需要在具體類中實現接口屬性如下。

[Table("TypeData.Country")] 
public class Country : BaseSqlEntity, ICountry 
{ 

    [Required, MaxLength(250)] 
    public string Name { get; set; } 

    [Required] 
    public string TwoLetterIsoCode { get; set; } 

    [Required] 
    public string ThreeLetterIsoCode { get; set; } 

    public int NumericIsoCode { get; set; } 

    public virtual List<StateProvince> StateProvinces { get; set; } 

    List<IStateProvince> ICountry.StateProvinces 
    { 
     get 
     { 
      return StateProvinces.ToList<IStateProvince>(); 
     } 

     set 
     { 
      StateProvinces = value.ToList().ConvertAll(o => (StateProvince)o); 
     } 
    } 
} 

[Table("TypeData.StateProvince")] 
public class StateProvince : BaseSqlEntity, IStateProvince 
{ 

    [Required, MaxLength(3)] 
    public string Abbreviation { get; set; } 

    [Required, MaxLength(250)] 
    public string Name { get; set; } 

    public Guid CountryId { get; set; } 

    [ForeignKey("CountryId")] 
    public virtual Country Country { get; set; } 

    ICountry IStateProvince.Country 
    { 
     get 
     { 
      return Country; 
     } 

     set 
     { 
      Country = (Country)value; 
     } 
    } 
} 

希望這會解決您的問題。

+0

感謝您的迴應!這樣可行。我真的希望有一個簡單的方法來做這樣的事情,比如[ForeignKey(「CountryId」),typeof(Country)],這會讓事情變得容易多了! –

相關問題