1
votes

I have the following:

public class Entity<TKey> : IEntity
{              
    [System.Runtime.Serialization.DataMemberAttribute]
    public virtual TKey Id { get; set; }
    [System.Runtime.Serialization.DataMemberAttribute]
    public virtual StateEnum State { get; set; }}


 public class Party : Entity<int>
{
    public virtual int TypeId { get; set; }
    public virtual string MobileNo1 { get; set; }}


public class Person : Party
{
    public virtual int PartyId { get; set; }
    public virtual string Name { get; set; }}

I have in sql Party table contains the following columns [Id,TypedId,MobileNo1] and Person table contains the following columns:[PartyId,Name] and PartyId is foreign key for Id of Party .

How to make the mapping ? I have tried but i'm not reach to any thing.

Edit

suppose that TKey is integer and the PartyId is key of person and foreign key to party

I try to make map for person

HasKey(p => p.PartyId);
ToTable("Person");

and to party

   HasKey(p => p.Id);
            ToTable("Party");

and its not work, how I make mapping in this case ?

Edit 2:

I have the following

 public class Entity<TKey> : IEntity
    {              
        [System.Runtime.Serialization.DataMemberAttribute]
        public virtual TKey Id { get; set; }
        [System.Runtime.Serialization.DataMemberAttribute]
        public virtual StateEnum State { get; set; }}


     public class Person : Entity<int>
    {
        public virtual string FirstName{ get; set; }
        public virtual string LastName{ get; set; }
}


    public class Employee: Person 
    {
        public virtual int PersonId { get; set; }
        public virtual string CompanyName{ get; set; }
}

I have Person table contain [Id,FirstName,LastName] I have Employee table contain [PersonId,FirstName,LastName]

-- PersonId is foreign key to person table. -- employee is a person the relationship is one to one. --for future i can add Student entity , so Student is person

How I can make mapping ?

1
Is there a reason an Employee should be both a person and have a link to a person? Why are you having PersonId on Employee? - Jcl

1 Answers

1
votes

I think you're trying to do something like this:

public class Entity<TKey> : IEntity
{              
[System.Runtime.Serialization.DataMemberAttribute]
public TKey Id { get; set; }
[System.Runtime.Serialization.DataMemberAttribute]
public StateEnum State { get; set; }}


public class Party : Entity<int>
{
public int TypeId { get; set; }
public string MobileNo1 { get; set; }

public virtual Person Person {get; set;}
}


public class Person
{
public int PartyId { get; set; }
public string Name { get; set; }

public virtual Party Party {get; set;}
}

So Person has a party (if it's optional you declare int?) and Party inherits from Entity. I also removed a lot of virtual declarations which probably make no sense (correct me if I'm wrong on some of them). The virtual keyword is used in Entity Framework to indicate a navigation property. So those properties are "links" to other existing entities. (Also assumed that PartyId is the primary key for Person).

I'm not sure this will work though because of the TKey primary key in Party (if you declare no primary key yourself EF automatically picks a property with "id" in the name) can be a different type and the foreignkey from Person is always an int... Why can't you just declare the Id as int?

EDIT: 1:1 relationship

modelBuilder.Entity<Person>()
            .ToTable("Person")
            .HasKey(p => p.PartyId)
            .HasRequired(p => p.Party)
            .WithRequiredDependent(p => p.Person)
            .Map(pers => pers.MapKey("PartyId");

That should normally work. It basically says Person needs to have a party and party needs a person. Because Person has the foreign key, it is the dependent.

You will also need to update your models without all the virtual properties and add virtual property to the corresponding entities (like in models I posted).It would be a lot easier to just use the attributes though and leave the fluent api as it is for this one:

public class Entity<TKey> : IEntity
{              
[System.Runtime.Serialization.DataMemberAttribute]
public TKey Id { get; set; }
[System.Runtime.Serialization.DataMemberAttribute]
public StateEnum State { get; set; }}


public class Party : Entity<int>
{
public int TypeId { get; set; }
public string MobileNo1 { get; set; }

public virtual Person Person {get; set;}
}


public class Person
{
[Key,ForeignKey("Party")]
public int PartyId { get; set; }
public string Name { get; set; }

public virtual Party Party {get; set;}
}