Entity Framework 4 v2 中与 POCO 的一对一关系

发布于 2024-08-18 10:53:44 字数 77 浏览 9 评论 0 原文

我一直在寻找有关如何在 EF4v2 中与 POCO 建立一对一关系的示例。我发现很多例子只展示了如何创建一对多或多对多。你有这方面的资源吗?

I've been looking for an example about how to build an one-to-one relationship in EF4v2 with POCO's. I found a lot of examples that show only how to create one-to-many or many-to-many. Do you have any resource about it?

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

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

发布评论

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

评论(3

意中人 2024-08-25 10:53:44

这对我有用。

数据库设计

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;

class Program
{
  static void Main(string[] args)
  {
    var myContext = new MyContext(@"Server=.\sqlexpress;Database=CodeFirst;integrated security=SSPI;");

    var fr = new FirstReading() { Increment = 12};
    myContext.Entry(fr).State = EntityState.Added;
    myContext.SaveChanges();

    var sr = new SecondReading() { Increment = 4 };
    sr.FirstReading = fr;
    myContext.SecondReading.Add(sr);
    myContext.SaveChanges();

    fr = myContext.FirstReading.Single(x => x.Increment == 12);
    Console.WriteLine(fr.Increment);
    Console.WriteLine(fr.SecondReading.Increment);
    sr = myContext.SecondReading.Single(x => x.Increment == 4);
    Console.WriteLine(sr.Increment);
    Console.WriteLine(sr.FirstReading.Increment);
    Console.ReadKey();
  }
}

public class FirstReading
{
  [Key][ForeignKey("SecondReading")]
  [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
  public int FirstReadingId { get; set; }
  public int Increment { get; set; }
  public virtual SecondReading SecondReading { get; set; }
}
public class SecondReading
{
  [Key]
  [ForeignKey("FirstReading")]
  public int FirstReadingId { get; set; }
  public int Increment { get; set; }
  public virtual FirstReading FirstReading { get; set; }
}

public class MyContext : DbContext
{
  public DbSet<FirstReading> FirstReading { get; set; }
  public DbSet<SecondReading> SecondReading { get; set; }

  public MyContext(string connectionString)
    : base(connectionString)
  {
    Database.SetInitializer<MyContext>(null);
  }

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

}

This worked for me.

Database design

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;

class Program
{
  static void Main(string[] args)
  {
    var myContext = new MyContext(@"Server=.\sqlexpress;Database=CodeFirst;integrated security=SSPI;");

    var fr = new FirstReading() { Increment = 12};
    myContext.Entry(fr).State = EntityState.Added;
    myContext.SaveChanges();

    var sr = new SecondReading() { Increment = 4 };
    sr.FirstReading = fr;
    myContext.SecondReading.Add(sr);
    myContext.SaveChanges();

    fr = myContext.FirstReading.Single(x => x.Increment == 12);
    Console.WriteLine(fr.Increment);
    Console.WriteLine(fr.SecondReading.Increment);
    sr = myContext.SecondReading.Single(x => x.Increment == 4);
    Console.WriteLine(sr.Increment);
    Console.WriteLine(sr.FirstReading.Increment);
    Console.ReadKey();
  }
}

public class FirstReading
{
  [Key][ForeignKey("SecondReading")]
  [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
  public int FirstReadingId { get; set; }
  public int Increment { get; set; }
  public virtual SecondReading SecondReading { get; set; }
}
public class SecondReading
{
  [Key]
  [ForeignKey("FirstReading")]
  public int FirstReadingId { get; set; }
  public int Increment { get; set; }
  public virtual FirstReading FirstReading { get; set; }
}

public class MyContext : DbContext
{
  public DbSet<FirstReading> FirstReading { get; set; }
  public DbSet<SecondReading> SecondReading { get; set; }

  public MyContext(string connectionString)
    : base(connectionString)
  {
    Database.SetInitializer<MyContext>(null);
  }

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

}
缱绻入梦 2024-08-25 10:53:44

查看客户 -> CustomerDetail >这个例子。这是 1:0..1,但我猜这样就可以了。

Look at Customer -> CustomerDetail in this example. This is 1:0..1, but I'm guessing that will do.

深海不蓝 2024-08-25 10:53:44

搜索此

如何使用 Entity Framework 4 Code First (POCO) 声明一对一关系

查看此链接

如何使用 Entity Framework 4 Code First (POCO) 声明一对一关系

search this

How to declare one to one relationship using Entity Framework 4 Code First (POCO)

or

look at this link

How to declare one to one relationship using Entity Framework 4 Code First (POCO)

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