Pages

Thursday, August 14, 2014

create Complex type in entity framework

Create calss:

public class Address//Complex type
    {
       public string Location { get; set; }
     
    }

 public class Person : IComparable
    {
        public long Id { get; set; }

        public long UserId { get; set; }//Mapping to User table

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }

        public long? OrganizationId { get; set; }

        public Organization Organization { get; set; }


     
        public User User { get; set; }

        public string Phone { get; set; }

        public Address Address { get; set; }///Mapping class
        public DateTime CreatedOn { get; set; }
       public DateTime ModifiedOn { get; set; }




        public int CompareTo(object obj)
        {
            throw new NotImplementedException();
        }
    }

In Db Context :  

modelBuilder.ComplexType<Address>();

When you go to use Complex Type with an existing database, you'll probably find your existing database won't support it. EF naming conventions mean the database is going to expect the Customer table corresponding to my Customer entity to have columns with the names  [Address_Location] and so on.

Enj..Hope it will help you ..all the best
         

No comments:

Post a Comment