实体框架 4.0 CTP5 一对一映射

发布于 2024-10-13 20:41:56 字数 2802 浏览 0 评论 0原文

您好,我正在使用 CTP5 在两个实体之间进行映射,如下所示:

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 isconfiguration

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");
    }
}

我做错了什么我的 User.UserType = null?

我到底该如何将其映射到工作!?

我要在这里死三天才能解决这个问题。

我正在使用 DevArt Connection 6.058...有些东西 Oracle 10g、C# EntityFramework 4.0

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃素的狼 2024-10-20 20:41:56

您已经在 User 和 UserType 之间设置了必需的关联,因此您不能拥有没有 UserType 的 User 对象(即 User.UserType == null)。为此,您需要对对象模型和 Fluent API 进行 2 项更改:

1. 将 UserTypeId 属性的类型更改为 int?

public int? UserTypeId { get; set; }

2. 删除来自 Fluent API 的代码如下:
HasRequired(p => p.UserType).WithMany().IsIndependent().Map(m => m.MapKey...

你不需要任何这些东西。一切都会由 Code First 根据您的约定进行配置。

You've setup a required association between User and UserType, therefore you cannot have a User object without a UserType (i.e. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文