locked
Map a entity's property directly to a property in a regular class? RRS feed

  • Question

  • Hello,

     

    I have a table called Entity1.  It has two fields in it called Id and Length which is a double.

    I am using the EF model first approach in my project.  And this is the code to my Entity1 class:

    public partial class Entity1
    
    {
      public Measurement measure;
    
      public Entity1()
    
      {
        measure = new Measumrent();
      }
    }
    
    

    This is my Measurement class:

    public class Measurement
    {
      public double Value {get; set;}
    
      // more stuff goes here
    }
    
    

    Now is there a way to directly map or databind directly so that the table's Length is mapped to the Value in Entity1.measure?

    If need be I would consider using a complextype or perhaps even EF code first.

     

    Thanks!

     

    Thursday, October 13, 2011 1:55 PM

Answers

  • Hi Lavagin,

    Thanks for your feedback.

    Have you install the EF4.1 code first here: http://blogs.msdn.com/b/adonet/archive/2011/04/11/ef-4-1-released.aspx

    You should add appropriate dlls to your project.---My suggestion is use ‘EntityFramework’ NuGet package to import the dlls.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity;
    
    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var context = new Context())
                {
                    context.Database.CreateIfNotExists();
                }
            }
        }
        [ComplexType]
        public class Measurement
        {
            public double Value { get; set; }
            // more stuff goes here
        }
        public partial class Entity1
        {
          [Key]
          public int ID{get;set;}
          public Measurement measure{get;set;}
          public Entity1()
          {
            measure = new Measurement();
          }
        }
    public class Context : DbContext
        {
            public DbSet<Entity1> Entity1s{ get; set; }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.ComplexType<Measurement>().Property(c=>c.Value).HasColumnName("Value");
            }
        }
    }
    
    

    This's my testing code and works ok on my computer.

    Have a nice day.

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alan_chen Monday, October 31, 2011 2:02 AM
    Wednesday, October 19, 2011 2:14 AM

All replies

  • Hi Lavagin,

    Welcome!

    If you update to EF 4.1 Code First, you can use Complex type like this:

    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity;
    
    [ComplexType]
    public class Measurement
    {
      public double Value {get; set;}
    
      // more stuff goes here
    }
    
    public partial class Entity1
    
    {
    [Key]
      public int ID{get;set;}
      public Measurement measure{get;set;};
    
      public Entity1()
    
      {
        measure = new Measumrent();
      }
    }
    public class Context : DbContext
        {
            public DbSet<Entity1> Entity1s{ get; set; }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.ComplexType<Measurement>().Property(c=>c.Value ).HasColumnName("Value ");
            }
        }
    --------------------------------------------
     class Program
        {
            static void Main(string[] args)
            {
                using (var context= new Context())
                {
                    context.Database.CreateIfNotExists();
                }
            }
        }
    

    Have a nice day. 


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, October 14, 2011 6:47 AM
  • I am not very familar witht he code first approach. 

    I keep getting this exception:

    "{"The entity type Entity1 is not part of the model for the current context."}"

     

    I am using this code in my using:

     

    using(var context = new Test5Entities())
    {
    context.Database.Connection.Open();
    context.Database.CreateIfNotExists();
    Entity1 e1 = new Entity1();
    e1.ID = 0;
    e1.measure.Value = 5.33;
    context.Entity1s.Add(e1);
    context.SaveChanges();
    }
    

    The exception occurs at this line:  context.Entity1s.Add(e1);

     

     

     

     

     

    Friday, October 14, 2011 6:19 PM
  • Hi,

    Please try in this way:

    using(var context = new Test5Entities())
    {
    context.Database.CreateIfNotExists();
    Entity1 e1 = new Entity1();
    e1.ID = 0;
    e1.measure= new Measurement(){Value=5.5}; 
    context.Entity1s.Add(e1);
    context.SaveChanges();
    }
    
    

    Have a nice day. 


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 17, 2011 1:52 AM
  • Examing the code...I discovered that the  code in the OnModelCreating method is never run.  Any idea why this isn't being called?
    Tuesday, October 18, 2011 1:08 PM
  • Hi Lavagin,

    Thanks for your feedback.

    Have you install the EF4.1 code first here: http://blogs.msdn.com/b/adonet/archive/2011/04/11/ef-4-1-released.aspx

    You should add appropriate dlls to your project.---My suggestion is use ‘EntityFramework’ NuGet package to import the dlls.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity;
    
    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var context = new Context())
                {
                    context.Database.CreateIfNotExists();
                }
            }
        }
        [ComplexType]
        public class Measurement
        {
            public double Value { get; set; }
            // more stuff goes here
        }
        public partial class Entity1
        {
          [Key]
          public int ID{get;set;}
          public Measurement measure{get;set;}
          public Entity1()
          {
            measure = new Measurement();
          }
        }
    public class Context : DbContext
        {
            public DbSet<Entity1> Entity1s{ get; set; }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.ComplexType<Measurement>().Property(c=>c.Value).HasColumnName("Value");
            }
        }
    }
    
    

    This's my testing code and works ok on my computer.

    Have a nice day.

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alan_chen Monday, October 31, 2011 2:02 AM
    Wednesday, October 19, 2011 2:14 AM
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 24, 2011 1:47 AM