visual studio - Multiple Inheritance (maybe Abstract class?) C# Entity Framework -
the last weeks have been working on development of database based on entity framework 6 (code-first) on c# using visual studio 2015.
i'm working on options inheritance offers work with. @ point , following standards of database should implement multiple inheritance primary key. means? means should implement class inherits class , have own pk identify him different parent class. @ point implement tpc, making parent class abstract , not defining pk on parent. sample of code @ point (this works , have tested it)
public abstract class person { public string name { get; set; } public string lastname { get; set; } } [table("students")] public class student : person { [key]public int id_student { get; set; } public string code_s { get; set; } public virtual icollection<course> courses { get; set; } }
then standard should follow build database requires me implement inheritance taking student parent , creating class inherits student. first , stupid idea had make simple write
childofchild : student
but didn't work.
then come mind possibility of make student class abstract, when declared student class abstract didn't let me instantiate , seed table. i'm not sure if there other way this, using abstract class or other method useful develop this. if didn't understand problem i'm trying solve sample of code have , works.
public abstract class person { public string name { get; set; } public string lastname { get; set; } } [table("students")] public class student : person { [key]public int id_student { get; set; } public string code_s { get; set; } public virtual icollection<course> courses { get; set; } } [table("exchangestudent")] public class exchangestudent : student { [key]public int id_exchange { get; set; } public string homeuniversity {get; set;} }
from understanding want 2 tables both own primary keys , foreign key define 1 one relationship. in c# code want these tables have inheritance relationship.
the way can see doing entity framework split requirements. firstly create database models represent table structure want.
namespace dao { public abstract class person { public string name { get; set; } } public class student { public int studentid { get; set; } public string nickname { get; set; } } //use navigation property rather inheritance define relationship public class exchangestudent { public int exchangestudentid { get; set; } public string homeuniversity { get; set; } public virtual student student { get; set; } } }
this gave me following code:
public override void up() { createtable( "dbo.exchangestudent", c => new { exchangestudentid = c.int(nullable: false, identity: true), homeuniversity = c.string(), student_studentid = c.int(), }) .primarykey(t => t.exchangestudentid) .foreignkey("dbo.student", t => t.student_studentid) .index(t => t.student_studentid); createtable( "dbo.student", c => new { studentid = c.int(nullable: false, identity: true), nickname = c.string(), name = c.string(), }) .primarykey(t => t.studentid); }
now if need have inheritance relationship between student , exchangestudent can create new objects define in different namespace
namespace businessobject { public abstract class person { public string name { get; set; } } public class student { public int studentid { get; set; } public string nickname { get; set; } } //exchangestudent derives student public class exchangestudent : student { public int exchangestudentid { get; set; } public string homeuniversity { get; set; } } }
then need create class maps 2 objects. there 3rd party libraries can well.
wiki
Comments
Post a Comment