简体   繁体   中英

Entity Framework 5 Code First using ComplexType Entity or standard one

I am working on an ASP.NET MVC 4 application using EF 5 and Code First . Part of my user interface are those three views :

简单菜单图片

Where each menu load a different information. However The information for both three menus represents a download link like this :

下载链接

It's just that some files are referred to Taxes other to Reports and some to Contracts but in the core it's the same download link, with the same info and all.

Due to the fact that there are a lot of similarities I decided to make my entity like this:

public class Menu
{
    public int MenuID { get; set; }

    public string Name { get; set; }

    public bool IsContract { get; set; }

    public bool IsTaxes { get; set; }

    public bool IsReport { get; set; }

    public int? ParentID { get; set; }

    public virtual ICollection<Document> Documents { get; set; }
}

Where Documents holds the information for the actual file like name and other stuff I need. But then I saw this option to use ComplexTypes and I start to wonder. Is it better to redo my entity using ComplexType and how exactly I can do this since I just saw the option but I'm not sure how exactly it's working.

The other option I see is just to have different entitues for Taxes , Reports and Contracts but this seems less optimal.

Complex types are a special kind of 1:0..1 mapping, so I don't see how it could apply to your 1:n association.

It certainly makes sense to subtype Document in Taxes , Reports , and Contracts if this is a more or less rigid classification. But if document types come and go, or documents frequently change type, subclassing is of no use. But anyhow it's the Document that knows its type , not a menu.

A Menu is a UI element and it should not contain Document s. A business domain should be completely independent of UI implementations.

In the controller you connect the business domain (the model) and the view. Thus, when the Contracts button is pressed I suppose the controller will collect contract documents. This could either be done by

context.Douments.OfType<Contract>()

or

context.Documents.Where(d => d.Type == "Contract")

depending on whether or not you will apply subtyping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM