c#-具有接口的实体框架代码优先迁移
作者:互联网
我正在尝试使用属性/注释方法(以接口作为属性类型)运行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();
}
}
我在上下文中添加了dbset,如下所示:
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并注释该界面,则它可以正常工作并正确生成关系.
有没有解决的办法?任何帮助将不胜感激.我们已经使用我们希望使用的接口构建了很多可重用的体系结构,但是看起来这可能是一个阻碍.
解决方法:
您仍然可以使用这些接口.
只需在下面的具体类中实现接口属性.
[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;
}
}
}
希望这能解决您的问题.
标签:ef-migrations,c,entity-framework 来源: https://codeday.me/bug/20191027/1946276.html