Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Complex type
Message
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Entity Framework
Titre:
Complex type
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01555361
Message ID:
01555361
Vues:
66
Hi everybody,


UPDATE. Never mind, solved it using the blog and fluent API mapping. However, now my controller doesn't seem to return result as quickly as it used to be (it doesn't return it at all), so I'm trying to debug and thus my next question.
--------------------------------------------------------------
I am re-factoring a bit my Clients class as I want to introduce separate EditorFor for the phone and Contact information. I introduced two new classes ContactDetail and PhoneInfo, but I am not sure how to incorporate them into Client so it will correctly map table columns?

Here is what I have at the moment
using System;

using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

using DataAnnotationsExtensions;
using System.ComponentModel.DataAnnotations.Schema;
using System.Collections.Generic;

namespace CardNumbers.Objects
{
      [ComplexType]
    public class PhoneInfo
    {
        [DataType(DataType.PhoneNumber)]
        [StringLength(10)]
        [DisplayName("Phone")]
        public virtual string Phone { get; set; }

        [StringLength(5)]
        [DisplayName("Ext")]
        public virtual string Ext { get; set; }

        public bool HasValue
        {
            get
            {
                return (Phone != null || Ext != null);
            }
        }

    }

    [ComplexType]
    public class ContactDetail
    {
        //Constructor
        public ContactDetail()
        {
            phoneInfo = new PhoneInfo();
        }

        [StringLength(100)]
        [DisplayName("Contact Name")]
        [DisplayFormat(NullDisplayText = "")]        
        public virtual string Contact { get; set; }

        [Email]
        [StringLength(100)]        
        [DisplayName("Email")]
        public virtual string Email { get; set; }

        public virtual PhoneInfo phoneInfo { get; set; }
        public bool HasValue
        {
            get
            {
                return (Contact != null || Email  != null || phoneInfo.HasValue);
            }
        }

    }

    public class Client
    {        
        [Key]
        [Column("ClientId",TypeName = "int")]
        public virtual int Id { get; set; }
        [Required]
        [DisplayName("Client No")]
        [Column("client_no", TypeName = "smallint")]
        public virtual Int16 Number { get; set; }
        
        [Required]
        [Column("client_name", TypeName = "varchar")]
        [DisplayName("Client Name")]
        [MaxLength(30, ErrorMessage = "Client Name should not be longer than 30 characters" )]
        [MinLength(3, ErrorMessage = "Client Name is too short")]
        public virtual string Name { get; set; }

        [StringLength(100)]
        [DisplayName("First Contact")]
        [DisplayFormat(NullDisplayText = "")]
        [Column("Contact1",TypeName =  "varchar")]
        public virtual string Contact1 { get; set; }

        [Email]
        [StringLength(100)]
        [Column("c1_email", TypeName = "varchar")]
        public virtual string Email1 { get; set; }

        [DataType(DataType.PhoneNumber)]
        [Column("C1_Phone", TypeName = "varchar")]
        [StringLength(10)]
        [DisplayName("Phone")]
        public virtual string Phone1 { get; set; }

        [StringLength(5)]
        [Column("C1_Ext", TypeName = "varchar")]
        [DisplayName("Ext")]
        public virtual string Ext1 { get; set; }

        [StringLength(100)]
        [DisplayName("Second Contact")]
        [Column("Contact2", TypeName = "varchar")]
        public virtual string Contact2 { get; set; }

        [Email]
        [StringLength(100)]
        [Column("C2_Email", TypeName = "varchar")]
        public virtual string Email2 { get; set; }

        [DataType(DataType.PhoneNumber)]
        [StringLength(10)]
        [DisplayName("Phone")]
        [Column("C2_Phone", TypeName = "varchar")]
        public virtual string Phone2 { get; set; }

        [StringLength(5)]
        [DisplayName("Ext")]
        [Column("C2_Ext",TypeName = "varchar")]
        public virtual string Ext2 { get; set; }

        [DataType(DataType.MultilineText)]
        public virtual string Address { get; set; }

        [ForeignKey("EnteredByOperator")]
        public string EnteredBy { get; set; }

        [InverseProperty("ClientsEnteredBy")]
        public virtual Operator EnteredByOperator { get; set; }

        [ForeignKey("ModifiedByOperator")]
        public string ModifiedBy { get; set; }

        [InverseProperty("ClientsUpdatedBy")]
        public virtual Operator ModifiedByOperator { get; set; }

        [DataType(DataType.DateTime)]
        [DisplayName("Created on")]
        public DateTime EnteredOn { get; set; }

        [DataType(DataType.DateTime)]
        [DisplayName("Modified on")]
        public DateTime? ModifiedOn { get; set; }

        public virtual ICollection<ClientOrder> ClientOrders { get; set; }
        
        public virtual ICollection<Reorder> Reorders { get; set; }
    }
}
Do you see what should I do to make contact1, email1, phone1 and Ext1 to map to the ContactDetail1 if I introduce this class?

Do you think I will be able to use Fluent API to map column names in my case? E.g. I found in the comments to the blog
public class EntityMappingContext : DbContext
{
    public DbSet<User> Users { get; set; } 

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new UserConfiguration());
    }
} 

class UserConfiguration : EntityTypeConfiguration<User>
{
    public UserConfiguration()
    {
        Property(u => u.DeliveryAddress.Street).HasColumnName("DeliveryAddress_Street");
        Property(u => u.DeliveryAddress.City).HasColumnName("DeliveryAddress_City");
        Property(u => u.DeliveryAddress.PostalCode).HasColumnName("DeliveryAddress_PostalCode"); 

        Property(u => u.HomeAddress.Street).HasColumnName("HomeAddress_Street");
        Property(u => u.HomeAddress.City).HasColumnName("HomeAddress_City");
        Property(u => u.HomeAddress.PostalCode).HasColumnName("HomeAddress_PostalCode");
    }
} 
So, is it what I will use if instead of my current code I'll go with public ContactDetail Contact1 {get;set;}

http://weblogs.asp.net/manavi/archive/2010/12/11/entity-association-mapping-with-code-first-part-1-one-to-one-associations.aspx

Is there something more recent on this topic?

Thanks.

Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Répondre
Fil
Voir

Click here to load this message in the networking platform