Sunday, 11 November 2012

Entity Framework Relationships?


yes, we can maintain relationships between the Entities like. Inhertience,Association(1-1,1-many,many-many) relationships.


Entity Framework Table per type:
Car-à CarId(PK),Brand,Model
BMWCars-> CarId(PK,FK) ,Region,BMWInfo
MaruthiCars->CarId(PK,FK),MaruthiInfo

                                   Then add the this  to ADO.NET Entity Framework.


first delete the associations between the tables.then add the Inhertience like below


First Delete the Association between the Cars— BMWCars, Cars—MaruthiCars, then Add Inhertice
Select---BMWCars-àBaseType : Cars
Select—MaruthiCar->BaseType :Cars
--------Inhertience is Added then Car is Base Type which has to share some Properties(CarID,Brand,Model ) to derived types(BMWCars,MaruthiCars) so Select Cars-----Abstract-àFlase(defult)—setàTrue
then Save.

Then
   using (EFRelationsEntities entitycontext = new EFRelationsEntities())
            {
                BMWCar bcar = new BMWCar { Brand = "BMWBrand1", Model = "BMW1", Region = "First", BMWCarsInfo = "Starts 25 laks",  };
               MaruthiCar mcar = new MaruthiCar { Brand = "Maruthi1", Model = "Maruthi1", MaruthiCarsInfo = "Starts 5 laks" };

              entitycontext.Cars.AddObject(bcar);
              entitycontext.Cars.AddObject(mcar);
               entitycontext.SaveChanges();
               Console.WriteLine("Cars are added Succefully...");
               Console.Read();
           }

Yes, Entity Framework make more work—for their apporiate data to their relavent data.
in Database :




this  is called Table per Entity relationship.

Table per Hirerchy:
Create Multiple Entities send data to Single table. as well update/ delete operations from their apporiate entities.



Set Car-----Abstract----True****

in database Car is single table but in Entity Framework which splicted as 2 Entities(BMWEntity,MaruthiEntity).


using (EFRelationsEntities entitycontext = new EFRelationsEntities())
            {

BMWEntity bentity = new BMWEntity { Brand = "Brandw1", Model = "Modelw1", BMWInfo = "starts 24 laks" };
 MaruthiEntity mentity = new MaruthiEntity { Brand = "Brandm1", Model = "Modelm1", MaruthiInfo = "starts 12 laks" };
    entitycontext.Cars.AddObject(bentity);
    entitycontext.Cars.AddObject(mentity);
     entitycontext.SaveChanges();
                Console.WriteLine(" Entities are not mapped");
                Console.Read();
           }

in databasse single table only




using Customized Entities[ BMWEntity, MaruthiEntity] :

we can update the database like


BMWEntity beneity = entitycontext.Cars.OfType<BMWEntity>().SingleOrDefault(p => p.Brand == "Brandw1");
      beneity.BMWInfo = "Cars is updated from customized entity BMWEnity";
                entitycontext.SaveChanges();
                Console.WriteLine(" Entities are not mapped");
                Console.Read();


now we can update/delete an apporiate database column values.

Independent Association( FK Association)



  Product p = new Product
                {
                   
                    ProductName = "Bovril",
                    Category = context.Categories
                                .Single(c => c.CategoryName == "Seafood")
                };
we are inserting a new Product by a referenced FK Association value.

Seafood Category is existed in Category table. by using any existing FK column we can able to 
insert or update or delete is Possible.

79 Bovril NULL 8 NULL NULL NULL NULL NULL 0




No comments:

Post a Comment