问题
I have a SQL database table which joins two other tables.
Classes (Table 1), Students (Table 2).
The joined table is called StudentClasses looks like: 1 Composite Primary Key made up of 2 columns: StudentID (from Students), ClassID (from Classes)
When I add my database to my C# project through ADO.NET EDMX entity framework database first approach, this joined table does not get added as an auto-generated cs class file, I suppose because it is made up of two foreign keys.
It does, however, get added to my EDMX XML information (see below extracted from EDMX).
My problem is that this table holds the information for any student being registered for a class. So if the student should be unregistered or registered for a class based on user input in my application, I cannot update the database through DbContextInstance.StudentClasses.Add(studentClassObject) or DbContextInstance.StudentClasses.Add(studentClassObject).
Is there some other way I should be updating this table in my C# code?
My Student class does have a property for Student.Classes, which allows me to manipulate that data within my program, but not to then push to the database (as far as I can tell).
I appreciate any help!
Below is the XML code which shows that the ADO.NET/EDMX is aware of the table, despite not creating an entity .cs auto-generated class.
<EntityType Name="StudentClasses">
<Key>
<PropertyRef Name="StudentID" />
<PropertyRef Name="ClassID" />
</Key>
<Property Name="StudentID" Type="int" Nullable="false" />
<Property Name="ClassID" Type="int" Nullable="false" />
</EntityType>
[...]
<EntitySet Name="StudentClasses" EntityType="Self.StudentClasses" Schema="dbo" store:Type="Tables" />
[...]
<Association Name="StudentClasses">
<End Role="Classes" Type="Self.Class" Multiplicity="*" />
<End Role="Students" Type="Self.Student" Multiplicity="*" />
</Association>
[...]
<AssociationSet Name="StudentClasses" Association="Self.StudentClasses">
<End Role="Classes" EntitySet="Classes" />
<End Role="Students" EntitySet="Students" />
</AssociationSet>
[...]
<AssociationSetMapping Name="StudentClasses" TypeName="ClassroomSchedulerModel.StudentClasses" StoreEntitySet="StudentClasses">
<EndProperty Name="Classes">
<ScalarProperty Name="ClassID" ColumnName="ClassID" />
</EndProperty>
<EndProperty Name="Students">
<ScalarProperty Name="StudentID" ColumnName="StudentID" />
</EndProperty>
</AssociationSetMapping>
回答1:
Yes there is, this table in entity framework is just navigation as ef is smart enough to detect that it is just a section table, the table will be represented per entity, so for each Student you will have
virtual Collection<Class> Classes
which represents all classes for this student, and vice versa, each class will have virtual Collection<Student> Students
which represents all students in this class.
to edit these you need to do something like this
// db is your database context
//Get Student
var theStudent= db.Students.FirstOrDefault(x => x.id == studentid);
//Get Class, include students with it
var theClass = db.Classes.Include(x => x.Students).FirstOrDefault(x => x.id == classid);
//Update Students collection of this class, add or remove
theClass.Students.Add(theStudent);
//or remove
theClass.Students.Remove(theStudent);
//save changes
db.SaveChanges();
// off topic but good to know
// you can also do things like this, get classes of this student
var ClassesStudent = db.Classes.where(x => x.Students.Any(s => s.id == studentid));
// or the Students of this Class
var StudentsClass = db.Students.Where(x => x.Classes.Any(c => c.id == classid));
I hope this is what you are looking for
来源:https://stackoverflow.com/questions/44235104/how-to-update-table-not-imported-by-edmx-in-c-sharp-project