EF 4.3 代码首先一对一关系

发布于 2025-01-06 15:05:12 字数 1501 浏览 1 评论 0原文

谁能告诉我下面这段代码有什么问题,因为我在首先使用 EF 4.3 代码获取一对一关系时遇到问题。

    // Problem with EF 4.3 code first, one-to-one relation

// context
// ------------
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{      
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    modelBuilder.Entity<SecondModel>().HasRequired(r => r.FirstModel).WithOptional(r => r.SecondModel).WillCascadeOnDelete(false);
}


// models
// --------

public abstract class MyBaseEntity : // some interfaces
{
    // ...

    [EdmScalarPropertyAttribute(EntityKeyProperty = true, IsNullable = false)]
    public virtual int Id
    {
        get { return GetPropertyValue<int>("Id"); }
        set { SetPropertyValue<int>("Id", value); }
    }

    // ...
}

public class FirstModel : MyBaseEntity
{
    // ...

    public int SecondModelID { get; set; }
    public virtual SecondModel SecondModel { get; set; }

    // ...
}

public class SecondModel : MyBaseEntity
{
    // ...

    public int FirstModelID
    {
        get { return GetPropertyValue<int>("FirstModelID"); }
        set { SetPropertyValue<int>("FirstModelID", value); }
    }
    public virtual FirstModel FirstModel { get; set; }

    // ...
}

// this code above doesn't seem to work :s
// when removing FirstModelID and removing SecondModelID i'm unable to create the the database

一直在尝试各种事情,添加外键属性,(取消)注释一些 id,遵循示例。 结果始终是:数据库中的 ID 不正确或未创建数据库。

提前致谢。

Could anybody tell me what's wrong with this code below, because I'm having problems getting an one-to-one relation working with EF 4.3 code first.

    // Problem with EF 4.3 code first, one-to-one relation

// context
// ------------
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{      
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    modelBuilder.Entity<SecondModel>().HasRequired(r => r.FirstModel).WithOptional(r => r.SecondModel).WillCascadeOnDelete(false);
}


// models
// --------

public abstract class MyBaseEntity : // some interfaces
{
    // ...

    [EdmScalarPropertyAttribute(EntityKeyProperty = true, IsNullable = false)]
    public virtual int Id
    {
        get { return GetPropertyValue<int>("Id"); }
        set { SetPropertyValue<int>("Id", value); }
    }

    // ...
}

public class FirstModel : MyBaseEntity
{
    // ...

    public int SecondModelID { get; set; }
    public virtual SecondModel SecondModel { get; set; }

    // ...
}

public class SecondModel : MyBaseEntity
{
    // ...

    public int FirstModelID
    {
        get { return GetPropertyValue<int>("FirstModelID"); }
        set { SetPropertyValue<int>("FirstModelID", value); }
    }
    public virtual FirstModel FirstModel { get; set; }

    // ...
}

// this code above doesn't seem to work :s
// when removing FirstModelID and removing SecondModelID i'm unable to create the the database

Have been trying all kinds of things, adding foreignkey attributes, (un)commenting some id's, following samples.
Results were always: IDs in database are not correctly or it doesn't create the database.

Thanks in advance.

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

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

发布评论

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

评论(1

善良天后 2025-01-13 15:05:12

我没有 EF 4.3,但如果它在这方面与 EF 4.1 不同,我会感到惊讶。我通过在 Fluent API 中配置模型、在 DbContext 类中重写 ModelBuilder 来完成此操作:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         modelBuilder.Entity<FirstModel>()
            .HasRequired(e => e.SecondModel)
            .WithRequiredPrincipal()
            .WillCascadeOnDelete(true);

            base.OnModelCreating(modelBuilder);

    }

以及类:

public class FirstModel : MyBaseEntity
{
// ...

public virtual SecondModel SecondModel { get; set; }

// ...
}

public class SecondModel : MyBaseEntity
{
// ...

public int Id
{
    get { return GetPropertyValue<int>("FirstModelID"); }
    set { SetPropertyValue<int>("FirstModelID", value); }
}


// ...
}

I dont have EF 4.3 but it is surprising if it is different from EF 4.1 in this regard. I've done it by configuring the model in the fluent API, overriding the ModelBuilder like this inside the DbContext class:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         modelBuilder.Entity<FirstModel>()
            .HasRequired(e => e.SecondModel)
            .WithRequiredPrincipal()
            .WillCascadeOnDelete(true);

            base.OnModelCreating(modelBuilder);

    }

And the classes:

public class FirstModel : MyBaseEntity
{
// ...

public virtual SecondModel SecondModel { get; set; }

// ...
}

public class SecondModel : MyBaseEntity
{
// ...

public int Id
{
    get { return GetPropertyValue<int>("FirstModelID"); }
    set { SetPropertyValue<int>("FirstModelID", value); }
}


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