gpt4 book ai didi

c# - 带有接口(interface)的 Entity Framework 代码优先迁移

转载 作者:行者123 更新时间:2023-11-30 14:51:41 24 4
gpt4 key购买 nike

我正在尝试使用属性/注释方法将接口(interface)作为属性类型来运行 EF 代码优先迁移。我们已经构建了一个带有接口(interface)的完整基础架构,并使用它们来实现具体类并希望启用迁移。 EF 似乎没有正确关联外键关系。有什么办法可以纠正这个问题吗?

这是一个例子:

我有一个 IStateProvince 接口(interface),如下所示:

 public interface IStateProvince
{

string Abbreviation { get; set; }

string Name { get; set; }

ICountry Country { get; set; }

}

我还有一个 ICountry 接口(interface)如下:

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 并注释界面,它工作正常并正确生成关系。

有解决办法吗?任何帮助将不胜感激。我们已经使用我们喜欢使用的界面构建了大量可重用的架构,但看起来这可能是一个阻碍。

最佳答案

您仍然可以使用接口(interface)。
只需要在具体类中实现接口(interface)属性如下。

[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;
}
}
}

希望这能解决您的问题。

关于c# - 带有接口(interface)的 Entity Framework 代码优先迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33749552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com