Página Inicial > Entity Framework, POCO > Entity Framework 4: auto relacionamento

Entity Framework 4: auto relacionamento

Pessoal vamos ver como construir um auto relacionamento no Entity Framework 4 + POCOs.

1-Classes

   1: public class Category

   2: {

   3:     public virtual int CategoryId { get; set; }

   4:     public String Name { get; set; }

   5:     public virtual Category Parent { get; set; }

   6:  

   7: }

Classe de categoria que possui uma auto-referencia

 

Pronto! todo o resto do código não é necessário.

2-Configuração

   1: public class CategoryConfiguration : EntityConfiguration<Category>

   2: {

   3:     public CategoryConfiguration()

   4:     {

   5:         Property(p => p.CategoryId).IsIdentity();

   6:     }

   7: }

 

3-DataContext

   1: public class DataContext

   2:     {

   3:  

   4:         public static Model FactoryDataContext()

   5:         {

   6:             String connection = @"Data Source=.\SQLEXPRESS;Initial Catalog=EFv4SelfReference;Integrated Security=SSPI;";

   7:             var builder = new ContextBuilder<Model>();

   8:             builder.Configurations.Add(new CategoryConfiguration());

   9:             return builder.Create(new SqlConnection(connection));

  10:         }

  11:  

  12:         public static void CreateDataBase()

  13:         {

  14:  

  15:             var ctx = FactoryDataContext();

  16:             if (ctx.DatabaseExists())

  17:                 ctx.DeleteDatabase();

  18:             ctx.CreateDatabase();

  19:  

  20:         }

  21:  

  22:     }

 

4-Model

   1: public class Model : ObjectContext

   2: {

   3:     public Model(EntityConnection entityConnection)

   4:         : base(entityConnection)

   5:     {

   6:         DefaultContainerName = "Model";

   7:     }

   8:     public IObjectSet<Category> Categories

   9:     {

  10:         get { return base.CreateObjectSet<Category>(); }

  11:     }

  12: }

5-Exemplo

   1: class Program

   2:    {

   3:        static void Main(string[] args)

   4:        {

   5:            DataContext.CreateDataBase();

   6:            Category category1 = new Category();

   7:            category1.Parent = null;

   8:            category1.Name = "cat1";

   9:            category1.Parent = new Category() { Name = "Parent1" };

  10:  

  11:            Model model = DataContext.FactoryDataContext();

  12:            model.Categories.AddObject(category1);

  13:            model.SaveChanges();

  14:  

  15:            foreach (var category in model.Categories)

  16:            {

  17:                Console.WriteLine("Category Name:" + category.Name + "\n");

  18:                if (category.Parent != null)

  19:                    Console.WriteLine("Parent Name:" + category.Parent.Name + "\n");

  20:                else

  21:                    Console.WriteLine("Parent Name: --");

  22:  

  23:                Console.WriteLine("\n\n");

  24:  

  25:  

  26:            }

  27:  

  28:            Console.ReadKey();

  29:        }

  30:  

  31:  

  32:    }

 

Codigo fonte

Author: higor.cesar Categories: Entity Framework, POCO Tags:
  1. Nenhum comentário ainda.
  1. Nenhum trackback ainda.