EF添加有外键model的问题

dlz1234 2014-10-12 09:56:01
有两个model,一对多关系的,我想添加数据岛关系为多的这张表,这张表有个外键,我要怎么操作,让这张表新增数据,不去新增外键所对应的表。
public class JournalSort
{
public int ID { get; set; }
public string JournalSortName { get; set; }
public virtual RootSort RootSorts { get; set; }
public List<JournalContent> JournalContents { get; set; }
}

public class RootSort
{
public int ID { get; set; }
public string RootSortName { get; set; }
public List<JournalSort> JournalSorts { get; set; }
}

两个模型如下

求高手解答 感激不尽

...全文
406 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dlz1234 2014-10-19
  • 打赏
  • 举报
回复
引用 2 楼 GREATCQI 的回复:
public class JournalSort { public int ID { get; set; } public string JournalSortName { get; set; } public virtual RootSort RootSorts { get; set; } public List<JournalContent> JournalContents { get; set; } } public class RootSort { public int ID { get; set; } public string RootSortName { get; set; } public List<JournalSort> JournalSorts { get; set; } } 看你的代码,下面的类型RootSort是主表,上面的是从表JournalSort。 主表创建数据的时候,本来就不会在从表中生成数据。 不是很明白你想描述的到底是什么场景?
JournalSort添加一条数据,同泛型的方法会在RootSort里也会自动创建一条数据。具体原因还不清楚。 现在解决了 谢谢各位。
Greatcqi 2014-10-12
  • 打赏
  • 举报
回复
public class JournalSort { public int ID { get; set; } public string JournalSortName { get; set; } public virtual RootSort RootSorts { get; set; } public List<JournalContent> JournalContents { get; set; } } public class RootSort { public int ID { get; set; } public string RootSortName { get; set; } public List<JournalSort> JournalSorts { get; set; } } 看你的代码,下面的类型RootSort是主表,上面的是从表JournalSort。 主表创建数据的时候,本来就不会在从表中生成数据。 不是很明白你想描述的到底是什么场景?
threenewbee 2014-10-12
  • 打赏
  • 举报
回复
ef会自动为你产生外键字段的。

110,570

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧