您需要更改生成表的名称:How to Specify Entity Framework Core Table Mapping?
任意使用:
[Table("CountriesCustomTableName")]
public class Country{ x, y, z }
或像这样覆盖 OnModelCreating:
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Country>(entity => {
entity.ToTable("CountriesCustomTableName");
});
}
记住要完全限定你的类名,否则你会感到困惑或出现模棱两可的编译错误
编辑
如果您尝试将 2 个不同的实体模型映射到同一张表,听起来您的模式或设计可能会有所改进。
我建议您要么需要使用完整的域模型,但在输入“半域模型”时只填充一半
public class Country {
public string X { get; set; }
public string Y { get; set; }
public string Z { get; set; }
}
var country1 = new Country() { X = "A", Y = "B", Z = "C" };
var country2 = new Country() { X = "A", Z = "C" };
context.Countries.Add(country1);
context.Countries.Add(country2);
或者(更有可能)这是某种形式的代码异味,您需要将可选值抽象到另一个表中,例如:
public class Country {
public string X { get; set; }
public string Z { get; set; }
}
public class CountryYInfo {
public string Y { get; set; }
public Country Country { get; set; }
public int CountryId { get; set; }
}
var country1 = new Country() { X = "A", Z = "C" };
var countryInfo = new CountryYInfo { Y = "B", Country = country1 };
var country2 = new Country() { X = "A", Z = "C" };
context.Countries.Add(country1);
context.Countries.Add(country2);