C# EntityCode First:创建了不需要的外键
C# EntityCode First : Unwanted foreign key created
我有一个 "Statements" 的 table 与两者相关:
- "Transactions" 的 table(在 IdStatement 上有一个外键)
- table 个 "SpecificTransactions"。 class "SpecificTransaction" 继承自 table "Transaction"
这是现有代码第一个模型:
Table声明
[Serializable]
[Table("Statement", Schema = "dbo")]
public class Statement
{
public Statement()
{
this.Transactions = new List<Transaction>();
this.SpecificTransactions = new List<SpecificTransaction>();
}
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int IdStatement { get; set; }
public List<Transaction> Transactions { get; set; }
public List<SpecificTransaction> SpecificTransactions { get; set; }
[... Other properties ...]
}
Table 交易
[Serializable]
[Table("Transaction", Schema = "dbo")]
public class Transaction
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int IdTransaction { get; set; }
[ForeignKey("Statement")]
public Nullable<int> IdStatement { get; set; }
[... Other properties ...]
}
Table SpecificTransaction(继承自Transaction)
[Serializable]
[Table("SpecificTransaction", Schema = "dbo")]
public class SpecificTransaction : Transaction
{
[StringLength(255)]
public string UniqueValue { get; set; }
[StringLength(50)]
public string Status { get; set; }
[StringLength(50)]
public string Hash { get; set; }
[... Other properties ...]
}
创建数据库时,在我的 table SpecificTransaction 中添加了一个不需要的字段(即列),称为 "Statement_IdStatement"。
当我尝试添加包含 SpecificTransactions 的语句时,none 以下列:
- Table 事务 > 列 IdStatement
- Table 特定交易 > 列 Statement_IdStatement
正在由 IdStatement 填充,我必须在之后添加 Id。
此外,当我在 table "SpecificTransaction" 上分配 "IdStatement" 时,只有 "Statement_IdStatement" 列被填充,而不是我的 "IdStatement" table声明。
有没有办法:
避免创建列 "Statement_IdStatement" 并以这种方式保持我的 EF 代码优先 class? (或者至少,table 的声明和交易没有变化)
当我添加包含 SpecificTransactions 的语句并保存更改时,列 "Transaction" > "IdStatement" 会自动填充 ?
提前致谢。
您正在获得额外的列 Statement_IdStatement
,因为您已经声明了第二个关系,直接在 Statement
和 SpecificTransaction
模型类型之间,即 属性 public List<SpecificTransaction> SpecificTransactions
在你的 Statement
class 上。这足以导致 Entity Framework 创建一个额外的 table 列来映射一对多关系,这就是它被填充的原因。它不需要映射到额外的外键模型 属性 即可发生。
您实际上可能不需要 Statement
上的额外 属性。实际上,您可以将 SpecificTransaction
个对象插入 Transactions
列表 属性,映射将为您处理。
您可能已声明此 属性,以便能够仅访问 Statement
的 SpecificTransaction
个对象。如果这是您的意图,您也可以使用 myStatement.Transactions.OfType<SpecificTransaction>()
.
访问它们
我有一个 "Statements" 的 table 与两者相关:
- "Transactions" 的 table(在 IdStatement 上有一个外键)
- table 个 "SpecificTransactions"。 class "SpecificTransaction" 继承自 table "Transaction"
这是现有代码第一个模型:
Table声明
[Serializable] [Table("Statement", Schema = "dbo")] public class Statement { public Statement() { this.Transactions = new List<Transaction>(); this.SpecificTransactions = new List<SpecificTransaction>(); } [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int IdStatement { get; set; } public List<Transaction> Transactions { get; set; } public List<SpecificTransaction> SpecificTransactions { get; set; } [... Other properties ...] }
Table 交易
[Serializable] [Table("Transaction", Schema = "dbo")] public class Transaction { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int IdTransaction { get; set; } [ForeignKey("Statement")] public Nullable<int> IdStatement { get; set; } [... Other properties ...] }
Table SpecificTransaction(继承自Transaction)
[Serializable] [Table("SpecificTransaction", Schema = "dbo")] public class SpecificTransaction : Transaction { [StringLength(255)] public string UniqueValue { get; set; } [StringLength(50)] public string Status { get; set; } [StringLength(50)] public string Hash { get; set; } [... Other properties ...] }
创建数据库时,在我的 table SpecificTransaction 中添加了一个不需要的字段(即列),称为 "Statement_IdStatement"。
当我尝试添加包含 SpecificTransactions 的语句时,none 以下列:
- Table 事务 > 列 IdStatement
- Table 特定交易 > 列 Statement_IdStatement
正在由 IdStatement 填充,我必须在之后添加 Id。
此外,当我在 table "SpecificTransaction" 上分配 "IdStatement" 时,只有 "Statement_IdStatement" 列被填充,而不是我的 "IdStatement" table声明。
有没有办法:
避免创建列 "Statement_IdStatement" 并以这种方式保持我的 EF 代码优先 class? (或者至少,table 的声明和交易没有变化)
当我添加包含 SpecificTransactions 的语句并保存更改时,列 "Transaction" > "IdStatement" 会自动填充 ?
提前致谢。
您正在获得额外的列 Statement_IdStatement
,因为您已经声明了第二个关系,直接在 Statement
和 SpecificTransaction
模型类型之间,即 属性 public List<SpecificTransaction> SpecificTransactions
在你的 Statement
class 上。这足以导致 Entity Framework 创建一个额外的 table 列来映射一对多关系,这就是它被填充的原因。它不需要映射到额外的外键模型 属性 即可发生。
您实际上可能不需要 Statement
上的额外 属性。实际上,您可以将 SpecificTransaction
个对象插入 Transactions
列表 属性,映射将为您处理。
您可能已声明此 属性,以便能够仅访问 Statement
的 SpecificTransaction
个对象。如果这是您的意图,您也可以使用 myStatement.Transactions.OfType<SpecificTransaction>()
.