Proper Code First EF6 structure for related classes/tables - c#

I have a two classes. A person class and a Title class.
public class Person
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int TitleTypeId { get; set; }
}
public class TitleType
{
public int Id { get; set; }
public string Name { get; set; }
}
The title class contains things like "Mr., Mrs., MS.,etc.."
What is the proper way to define these classes so that I can use Linq to recall the desired title "name" from the TitleType table?
i.e. person.Title.name

public class Person
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public TitleType Title { get; set; }
}
EF will create additional column to handle Foreign Key for you automatically.

Related

How to create field and get data from joined table in Entity Framework 6

I'm using EF6 Code First to work.
First I created 2 class:
public class Course
{
public int Id { get; set; }
public string Title { get; set; }
public ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public int Id { get; set; }
public string Name{ get; set; }
public ICollection<Course> Courses { get; set; }
}
After add-migration and update-database, I have 3 table Courses, Tags and TagCourses. But I dont have TagCourse class in project. How can I add new fields like Details on TagCourses and CRUD this joined table?
This might work for you
public class Course
{
public int Id { get; set; }
public string Title { get; set; }
public ICollection<TagCourse> TagCourses{ get; set; }
}
public class TagCourse
{
public int CourseId {get;set; }
public int TagId {get;set; }
public string Details {get;set; }
}
public class Tag
{
public int Id { get; set; }
public string Name{ get; set; }
public ICollection<TagCourse> TagCourses { get; set; }
}

Club the UI model and DB model in C#

I'm having a client, and it sending the following Signature to the Library
Client UI Signature :
namespace Library.Model
{
public class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
public string streetName { get; set; }
public string City { get; set; }
public string State { get; set; }
}
}
Library DB Structure:
namespace Library.Data
{
public class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
public int AddressId { get; set; }
public Address AddressInfo { get; set; }
}
public class Address
{
public int AddressId { get; set; }
public string streetName { get; set; }
public string City { get; set; }
public string State { get; set; }
}
}
Here I'm doing the mapping process from Client UI model to DB Structured model. How could I use the DB structured model as like Client model instead of the Client model.
Kindly assist me how efficiently we can share the DB Structured model in Client?
Note: But the Client the Signature should be
public class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
public string streetName { get; set; }
public string City { get; set; }
public string State { get; set; }
}
Kindly refer Update a class property based on another Property of a calss properties value in the Setter - I need the solution similar to this.
i think you can use PersonViewModel to make this and it will be like you mention
public class PersonViewModel
{
public int PersonId { get; set; }
public string Name { get; set; }
public string streetName { get; set; }
public string City { get; set; }
public string State { get; set; }
}
Make join and file this object
PersonViewModel persons = new PersonViewModel ();
I wish it will help you :)

Ef6 Cast an Interface type to use as collection

Hello all I am trying to figure out how to do the following. I have many Entities that will use the following pattern and am not sure the best way to impalement it.
I will use dummy classes to demonstrate what i am trying to accomplish.
first thing is first we have methods that take an interface called IClass
public interface IClass
{
string Title { get; set; }
int Id { get; set; }
ICollection<IPerson> People{get;set;}
}
public class StudentClass:IClass
{
public string Title { get; set; }
public int Id { get; set; }
public ICollection<IPerson> People { get; set; }
}
public class TeacherClass : IClass
{
public string Title { get; set; }
public int Id { get; set; }
public ICollection<IPerson> People { get; set; }
}
public class Employee : IPerson
{
public string FirstName { get; set; }
public int Id { get; set; }
public int EmployeeNumber { get; set; }
public string LastName { get; set; }
}
public class Student : IPerson
{
public string FirstName { get; set; }
public int Id { get; set; }
public string LastName { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
What I would like to do is use modelBuilder to register ICollection<IPerson> specific to that class. For example in the TeacherClass IPerson should be Employee and StudentClass it should be Student
How can I accomplish this?

Entity Framework Multi Table Relation

I have three tables
EVENT - PERSON - COMPANY
I need to have a relation many-to-many using those tables. An event can have one or more "clients",which can be either person or company. Normally, using no ORM, using sql , it would be something like :
EVENT
----
id
name
CLIENTEVENT
-----------
id
clientid
clienttype -- person or company
PERSON
-----------
id
name
lastname
...
COMPANY
-------
id
name
How does this approach can be replicated using entity framework? I am pretty new using EF so I would appreciate all help you can give me.I am using repository pattern, following this approach http://www.codeproject.com/Articles/838097/CRUD-Operations-Using-the-Generic-Repository-Pat.
I advice you instead of clientid and clienttype make another two columns: personID and companyID. Models will look this way:
public class Event
{
public int ID { get; set; }
public string name { get; set; }
public virtual ICollection<CLIENTEVENT> links { get; set; }
}
public class Company
{
public int ID { get; set; }
public string name { get; set; }
public virtual ICollection<CLIENTEVENT> links { get; set; }
}
public class PERSON
{
public int ID { get; set; }
public string name { get; set; }
public string lastname { get; set; }
public virtual ICollection<CLIENTEVENT> links { get; set; }
}
public class CLIENTEVENT
{
public int ID { get; set; }
public virtual PERSON person { get; set; }
public int? personID { get; set; }
public virtual Company company { get; set; }
public int? companyID { get; set; }
public virtual Event event1 { get; set; }
public int? event1ID { get; set; }
}
With EF Code First, you don't have to deal with joining tables like ClientEvent. You can simply write:
public class Event
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Company> Companies { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Event> Events { get; set; }
}
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public string Lastname { get; set; }
public virtual ICollection<Event> Events { get; set; }
}
Try this code, you'll see EF creating two linking tables (EventPerson and EventCompany). Now, the client type is the collection you read (someEvent.Persons or someEvent.Companies), you can also easily get the events linked to a specific Person or a Company.
About many-to-many with EF.

Foreign Key References to Primary Key in the Same Table EF6

I have class in my MVC project and I used Entity Framework 6. Every person has a Master (master_Id) and it references to the same table (primary key in Person table). My way does not work... what's the solution?
public class Person
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
[ForeignKey("Person")]
public int Master_Id { get; set; }
public virtual Person Master { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}
public class Person
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public int? MasterId { get; set; }
[ForeignKey("MasterId")]
public virtual Person Master { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}

Categories

Resources