Entity Framework 带接口的代码优先迁移
Entity Framework Code First Migrations with Interfaces
我正在尝试 运行 使用 Attribute/annotations 方法将接口作为 属性 类型进行 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; }
在 运行ning 迁移后,我得到以下信息:
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;
}
}
}
希望这能解决您的问题。
我正在尝试 运行 使用 Attribute/annotations 方法将接口作为 属性 类型进行 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; }
在 运行ning 迁移后,我得到以下信息:
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;
}
}
}
希望这能解决您的问题。