简体   繁体   中英

Entity Framework 4.0 CTP5 One to One Mapping

Hi i am using CTP5 to map between two entities like that:

public class User
{
    public int Id { get; set; }

    public string UserName { get; set; }
    public string Password { get; set; }
    public bool IsManager { get; set; }
    public decimal Credit { get; set; }
    public int CreditAlertCount { get; set; }
    public decimal TelPrice { get; set; }
    public decimal CellPrice { get; set; }
    public DateTime InsertDate { get; set; }
    public IList<string> PhoneList { get; set; }
    public int UserTypeId { get; set; }
    public virtual UserType UserType { get; set; }
}

public class UserType
{
    public int Id { get; set; }
    public int UserLevel { get; set; }
    public string TypeDescription { get; set; }
}

//here is configurations

public class UserConfig : EntityTypeConfiguration<User>
{
    public UserConfig()
    {

        HasKey(c => c.Id);
        Property(c => c.Id).HasDatabaseGenerationOption(DatabaseGenerationOption.Identity).HasColumnName("ID");
        Property(c => c.InsertDate).HasDatabaseGenerationOption(DatabaseGenerationOption.Computed).HasColumnName("INSERT_DATE");
        Property(c => c.IsManager).HasDatabaseGenerationOption(DatabaseGenerationOption.Computed).HasColumnName("IS_MANAGER");
        Property(c => c.UserName).HasMaxLength(25).IsRequired().HasColumnName("USER_NAME");
        Property(c => c.Password).HasMaxLength(25).IsRequired().HasColumnName("USER_PASSWORD");
        Property(c => c.CellPrice).IsRequired().HasColumnName("CELL_PRICE");
        Property(c => c.TelPrice).IsRequired().HasColumnName("TEL_PRICE");
        Property(c => c.CreditAlertCount).IsRequired().HasColumnName("CREDIT_ALERT_COUNT");
        Property(c => c.Credit).IsRequired().HasColumnName("CREDIT");
        Property(c => c.UserTypeId).IsOptional().HasColumnName("USER_TYPE_ID");

        /*relationship*/
        HasRequired(p => p.UserType).WithMany().IsIndependent().Map(m => m.MapKey(p => p.Id, "USER_TYPE_ID"));

        ToTable("CRMC_USERS", "GMATEST");
    }
}

 public class UserTypeConfig : EntityTypeConfiguration<UserType>
{
    public UserTypeConfig()
    {
        /*Identity*/
        HasKey(c => c.Id);
        Property(c => c.Id).HasDatabaseGenerationOption(DatabaseGenerationOption.Identity).HasColumnName("ID");

        /*simple scalars*/
        Property(s => s.TypeDescription).IsRequired().HasColumnName("DESCRITPION");
        Property(s => s.UserLevel).IsRequired().HasColumnName("USER_LEVEL");

        ToTable("CRMC_USER_TYPES", "GMATEST");
    }
}

What do i do wrong my User.UserType = null?

How to hell do i map this to work!?

I am dying here for 3 days to work it off.

I'm using DevArt Connection 6.058... some thing Oracle 10g, C# EntityFramework 4.0

You've setup a required association between User and UserType, therefore you cannot have a User object without a UserType (ie User.UserType == null). To be able to do that you need to make 2 changes to your object model and fluent API:

1.Change the type of UserTypeId property to int? :

public int? UserTypeId { get; set; }

2.Remove the code from your fluent API that reads: HasRequired(p => p.UserType).WithMany().IsIndependent().Map(m => m.MapKey...

You don't need any of those stuff. Everything will be configured by Code First based on convention for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM