none
CustomAssociationAttribute RRS feed

  • Question

  • Hello,

    I have a problem with Using EF and association for a specific scenario.

    I usually put all my picklists in a single SQL table with 3 keys and the Label:

    • ClassName (string)
    • PropertyName(string)
    • Value(int)

    In SQL I have no problem for getting the value.

    EF How can I retrieve the label corresponding without adding for each class a property ClassName and a property with the propertyName.

     

    Wednesday, June 15, 2011 4:35 PM

All replies

  • Hello Benjamin,

    Thanks for your post.

    According to your description, I don't understand well what you want to do, could you please show me some code of yours? Such as SQL script or your classes, and so on. And show me more details about what you want.

    I am looking forward to hearing from you.

     

    Have a nice day,


    Jackie Sun [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, June 17, 2011 7:33 AM
    Moderator
  • Here is some code

      public class PicklistData
      {
        [Key]
        public string ClassName { get; set; }
    
        [Key]
        public string PropertyName { get; set; }
    
        [Key]
        public int Value { get; set; }
    
        public string Label { get; set; }
      }
    
      public class UserInformation
      {
        [Key]
        public int UserId { get; set; }
        public string FirstName { get; set; }
    
        public string LastName { get; set; }
    
        public int Civility { get; set; }
    
        [Association("Civility_PickList", "UserInformation,CivilityLabel,Civility", "ClassName,PropertyName,Value", IsForeignKey = true)]
        public virtual PicklistData CivilityValue { get; set; }
    
        public int Country { get; set; }
    
        [Association("Country_PickList", "UserInformation,CountryLabel,Country", "ClassName,PropertyName,Value", IsForeignKey = true)]
        public virtual PicklistData CountryValue { get; set; }
    
    
      }
    
      public class SampleData
      {
        private List<UserInformation> _sampleUsers;
    
        private List<PicklistData> _samplePickList;
    
        public SampleData()
        {
          _samplePickList = new List<PicklistData>();
    
          _samplePickList.Add(new PicklistData { ClassName = "UserInformation", PropertyName = "CivilityLabel", Value = 1, Label = "Mister"});
    
          _samplePickList.Add(new PicklistData { ClassName = "UserInformation", PropertyName = "CivilityLabel", Value = 2, Label = "Miss" });
    
          _samplePickList.Add(new PicklistData { ClassName = "UserInformation", PropertyName = "CountryLabel", Value = 1, Label = "US" });
    
          _samplePickList.Add(new PicklistData { ClassName = "UserInformation", PropertyName = "CountryLabel", Value = 2, Label = "UK" });
    
          _sampleUsers = new List<UserInformation>();
          
          _sampleUsers.Add(new UserInformation {FirstName = "John", LastName = "Doe",Civility = 1,Country = 1});
    
        }
      }


     I would like having only one SQL Table for the PicklistData and Get my association and be able to order by the CivilityValue.

    As the AssociationAttribute don't work the way I writed it for sample. I see another way still with one SQL Table for PicklistData, but a class for each Class/Property

     

      public class CivilityPickList
      {
        
      }
    
      public class CountryPicklist
      {
        
      }
    
      public class UserInformation
      {
        [Key]
        public int UserId { get; set; }
        public string FirstName { get; set; }
    
        public string LastName { get; set; }
    
        public int Civility { get; set; }
    
        [Association("Civility_PickList", "Civility", "Value", IsForeignKey = true)]
        public virtual CivilityPickList CivilityValue { get; set; }
    
        public int Country { get; set; }
    
        [Association("Country_PickList", ",Country", "Value", IsForeignKey = true)]
        public virtual CountryPicklist CountryValue { get; set; }
    
    
      }

     

    But I don't know how to configure the modelbuilder

     

    Thanks.


    Friday, June 17, 2011 12:07 PM
  • Hi Benjamin,

    Thanks for your feedbacks.

    You mean, you have some classes, in which there are some same properties, am I right? I think you can use table splite. To make the same properties in one entity, and the other entities can has related with this entity, in the SQL Client, there's no additional table. Like the following code:

    (Just show an example)

     

        public class Employee
        {
            public int EmployeeID { get; set; }
            public string LastName { get; set; }
            public string FirstName { get; set; }
            public string Title { get; set; }
            public string TitleOfCourtesy { get; set; }
            public DateTime? BirthDate { get; set; }
            public DateTime? HireDate { get; set; }
            public string Address { get; set; }
            public string City { get; set; }
            public string Region { get; set; }
            public string PostalCode { get; set; }
            public string Country { get; set; }
            public string HomePhone { get; set; }
            public string Extension { get; set; }
            public string Notes { get; set; }
            public int? ReportsTo { get; set; }

            public virtual EmployeePhoto EmployeePhoto { get; set; }
        }

        public class Customer
        {
            public int CustomerID { get; set; }
            public string Name { get; set; }

            public virtual EmployeePhoto EmployeePhoto { get; set; }
        }

        public class EmployeePhoto
        {
            [Key]
            public int EmployeeID { get; set; }
            public byte[] Photo { get; set; }
            public string PhotoPath { get; set; }

            public virtual Employee Employee { get; set; }
            public virtual Customer Customer { get; set; }
        }

        public class NorthwindContext : DbContext
        {
            public NorthwindContext()
                : base("connection")
            { }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<EmployeePhoto>()
                            .HasRequired(e => e.Employee)
                            .WithRequiredDependent(e => e.EmployeePhoto);

                modelBuilder.Entity<Employee>().ToTable("Employees");
                modelBuilder.Entity<EmployeePhoto>().ToTable("Employees");

                modelBuilder.Entity<EmployeePhoto>()
                    .HasRequired(c => c.Customer)
                    .WithRequiredDependent(e => e.EmployeePhoto);
                modelBuilder.Entity<EmployeePhoto>().ToTable("Customer");
                modelBuilder.Entity<Customer>().ToTable("Customer");
            }

            public DbSet<Employee> Employees { get; set; }
            public DbSet<EmployeePhoto> EmployeePhoto { get; set; }
            public DbSet<Customer> Customers { get; set; }
        }

    I hope this can help you.

     

    Have a nice day,


    Jackie Sun [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, June 20, 2011 5:18 AM
    Moderator
  • Thanks Jackie,

    I think what I need is TPH. But I don't see how use it has I have 2 discriminator.

    Monday, June 20, 2011 9:52 AM