English 中文(简体)
A. 实体一至多
原标题:Entity One to Many Db

我有一个基本问题,即执行实体法,首先是一至多条。 我有两种模式:文章和评论。

public class Article
    {
    public int Id { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }
....

 public class Comment
{
    public int Id { get; set; }

    public virtual Article CommentForArticle { get; set; }

    public DateTime TimeStamp { get; set; 
....

如果我试图在Db中添加评注(包含该条的评注)。 该评论在数据库中添加,但该条并不包含在收集意见中的评论。

但是,如果我发表评论,在文章中添加,一切都行不通。 我没有必要在数据库中添加评论,而是自动添加(我猜测)。

我在试图落实这一问题(如:多重限制被违反)之后遇到了问题,因此,我的问题是,现在我是这样做的,为什么你必须这样做,而不是增加Db的评论,而是对载有评论的该条的评论。 你们能够分享任何可能帮助我更好地了解实体框架如何与一种关系和许多关系打交道的联系。

Thanks, Garrett

最佳回答

如果存在“旧”关系,我更愿意向所有人收取新物品。 在您的情形下,Article hasComments,因此,我要补充一条新的评注。 评论。

除继承外,还可能建立“主体”关系。 附录 类型。 在这种情况下,我倾向于确定财产。 我甚至不喜欢<条码>,即有<条码>的导航财产。

In case of "pure" many-to-many (i.e. when the join table is not mapped as an entity type of its own) there is no other alternative than adding items to owner s collections.

It is not clear why you had problem after problem. Is it because you mixed both approaches? (adding Comments to the db and adding the same comments to the article).

参考:大量文章开始here。 它侧重于比较复杂的假设情景,但提及经常案件并非难以找到。

问题回答

暂无回答




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签