简体   繁体   中英

Extract one specific entry from a table?

I am currently looking for a way I can pass a foreing key to a table entry that is listed in one table, and should be extracted in another table.

for example purposes I created this ?

public class Parent 
{
    public string Name {get; set;}
    public virtual ICollection<child> Children
    Public virtual ICollection<School> Schools {get; set;}
}

public class Child 
{
    public string Name {get; set;}
    Public School Schoola{get; set;} // Which should be a school Name that the Parent Should know?
}


public class School
{
    //ParentID
    //ChildID
    public string SchoolName {get; set;}
}

在此处输入图片说明

How do i give my Child instance a SchoolName that the Parent contains within the SchoolNames?

Children and SchoolNames are seperate tables - but child only need to know a specific entry..

Caveat

Your code does not work, since EF does not serialize collections of primitive types . EF Core does have value conversions but it is unclear what you're exactly looking for. I'm going to assume you meant to store these as actual School entities, since your question asks how to "extract one entry from a table ".

For the sake of answering your question, I assume that your child should have a reference to the school entity, not a string property that's technically unrelated to the school entity itself, which would make it a question not related to Entity Framework and thus the question tags would be wrong.

I'll address both my assumption and your literal question, just to be sure.


If you need a relationship between a child and a school

From a purely database standpoint, there is no way to specify that an entity's (Child) foreign key should refer to an entity (School) which in and of itself has a foreign key to another entity (Parent). It simply doesn't exist in SQL and therefore EF cannot generate this behavior for you.

What you can do, is implement business validation on your code and refuse to store any child with a school that doesn't belong to its parent. Keep in mind, this requires you to load the parent and their schools every time you want to save a child to the database (because otherwise you can't check if the selected school is allowed for this child), so it will become a somewhat expensive operation.

However, that doesn't prevent the possibility for someone to introduce data into the database (circumventing your business logic, eg by a DBA) where this rule is violated but the FK constraint itself is upheld.
How you handle these bad data states is up to you. Do you remove those entries when you stumble upon them? Do you proactively scan the database once in a while? Do you allow it to exist but restrict your application's users to only choosing schools from the parent's scope? These are all business decisions that we cannot make for you.


If a child needs a school name without a relation to the school itself

At first sight, this seems to me to be a bad solution. What happens when the school's name changes? Wouldn't you expect the child's schoolname to also change? Because that's not going to happen in your current setup.

In either case, if you are looking to set a string property, that's trivial, you simply set the property. Presumably, your question is how to restrict the user's options to the child's parent's schools.

This restrictive list can be fetched from the database using the child's identifier:

var childID = 123;

var schoolsFromParent = db
                         .Children
                         .Where(c => c.Id == childId)
                         .Select(c => c.Parent.Schools)
                         .FirstOrDefault();

Note that this code works regardless of whether you have a School entity or a list of strings - though the type of schoolsFromParent will be different.

And then restrict your end user to only being able to pick from the presented options. Note that to prevent bad data, you should doublecheck the chosen name after the user has selected it.

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