【发布时间】:2021-07-01 15:49:07
【问题描述】:
我尝试通过让翻译模型运行
public class Item
{
public Item
{
TextTranslationID = Guid.NewGuid();
DescriptionTranslationID = Guid.NewGuid();
TextTranslations = new HashSet<Translation>();
DescriptionTranslations = new HashSet<Translation>();
}
[Key]
public int ItemID { get; set; }
public Guid TextTranslationID { get; set; }
public Guid DescriptionTranslationID { get; set; }
[ForeignKey(nameof(TextTranslationID))]
public virtual ICollection<Translation> TextTranslations { get; set; }
[ForeignKey(nameof(DescriptionTranslationID))]
public virtual ICollection<Translation> DescriptionTranslations { get; set; }
}
和翻译实体
public class Translation
{
public Translation()
{
UniqueTranslationID = Guid.NewGuid();
}
[Key]
public Guid UniqueTranslationID { get; set; }
/// <summary>
/// The translation id, keyed with the language.
/// </summary>
[Required]
public Guid TranslationID { get; set; }
/// <summary>
/// The 2-char language code. eg "en", "es"
/// </summary>
[Required]
[StringLength(2, MinimumLength = 2)]
public string Language { get; set; }
[Required]
[StringLength(2000)]
public string Text { get; set; }
}
这是一个单向关系,因此我不需要也不想要 Translation.Parent 或 Translation 实体上的类似内容。
Item 实体只是翻译的众多消费者之一,因此这里不需要逆属性。
如您所见,该项目与翻译有两个连接。
我已经尝试了这么多与modelbuilder的组合来在sql中完成这样一个简单的任务,但是生成的sql脚本总是想在翻译表中添加一个DescriptionTranslationID和TextTranslationID。
...
migrationBuilder.CreateTable(
name: "Translations",
columns: table => new
{
UniqueTranslationID = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
TranslationID = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Language = table.Column<string>(type: "nvarchar(2)", maxLength: 2, nullable: false),
Text = table.Column<string>(type: "nvarchar(2000)", maxLength: 2000, nullable: false),
DescriptionTranslationID = table.Column<int>(type: "int", nullable: true),
TextTranslationID = table.Column<int>(type: "int", nullable: true)
},
...
如何设置从项目到翻译的两个一对多关系? 非常感谢!
【问题讨论】: