开发者

Entity Framework Class / Database Schema advice needed

I asked a question recently, and quite frankly, from the answer that was given, I am second guessing my entire strategy/how I design the classes and database.

I have not yet used the virtual keyword, nor Icollection at all in any of my Entity Framework projects, and quite frankly, after reading about it in some of the examples I do not fully understand why it is needed, or how it works.

In a sample application, I have a simple design where there are three lists - people, notes and pictures. The relationships are such that a person can own multiple notes and pictures, as well as people having a logo which is a picture.

   public class Person
    {
        public int ID { get; set; }
        public string name { get; set; }
        public Picture logo { get; set; }
    }

    public class Note
    {
        public int ID { get; set; }
        public string Text { get; set; }
        public Person Owner { get; set; }
    }

 public class Picture
    {
        public int ID { get; set; }
        public string Path { get; set; }
        public Person Owner { get; set; }
    }

When I want to select a list of notes that a person owns, I simply perform db.Notes.Where(x=>x.owner=="y") on the notes object. I think I understand that if I were to use Icollection on the person class, I could instead perform something along the lines of db.person.select(x=> x.notes) to retrieve all the notes. Am I correct in this thinking?

If you were in my position with the relatively simple example above, how would you build the classes (involving ICollection, virtual or anything else)?

In addition and most importantly, the above is just an example, however in my actual application, I have used a very similar structure where I use my custom type as the "connector"/Foreign Key.

In many examples I have been reading, (in the above example) they would be using public int OwnerID instead of public person Owner. This has really thrown me and I am questioning my entire EF stra开发者_JS百科tegy. What are the differences?

Any advice would be greatly appreciated.


I think you are making this more difficult that is needed. If you were laying out regular classes you would relate them to each other rather than finding related id's and loading them separately which you are doing in your example.

public class Person 
{ 
    public int ID { get; set; } 
    public string name { get; set; } 
    public ICollection<Note> Notes { get; set; }
    public ICollection<Picture> Pictures { get; set; }
    public Picture logo { get; set; } 
} 

public class Note 
{ 
    public int ID { get; set; } 
    public string Text { get; set; } 
    public Person Owner { get; set; } 
} 

public class Picture 
{ 
    public int ID { get; set; } 
    public string Path { get; set; } 
    public Person Owner { get; set; } 
} 

So now say you have gotten your person object using the query

var person = _context.People.Where(m=>m.ID=randomIntWeWant).First();

We can get all related items as properties.

For Notes

person.Notes

For Photos

person.Photos

ICollection is related to lazy loading. By declaring a property as ICollection on one side, your are saying you have a many-to-one relationship between the objects. If you declare a property as ICollection on both sides, you are saying it is a many-to-many relationship. EF takes care of creating the tables that track that relationship.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜