Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何与现有字段建立一对一关系?_C#_.net_Entity Framework 5 - Fatal编程技术网

C# 如何与现有字段建立一对一关系?

C# 如何与现有字段建立一对一关系?,c#,.net,entity-framework-5,C#,.net,Entity Framework 5,我有3个实体: Foo Bar UniqueFooBar Foo和Bar是如下实体: public class Bar { public int Id {get; set;} // inverse nav property public virtual UniqueFooBar UniqueFooBar {get; set;} } public class Foo { public string Name {get; set;} // inverse

我有3个实体:

  • Foo
  • Bar
  • UniqueFooBar
Foo
Bar
是如下实体:

public class Bar {

   public int Id {get; set;}

   // inverse nav property
   public virtual UniqueFooBar UniqueFooBar {get; set;}

}

public class Foo {

   public string Name {get; set;}

   // inverse nav property
   public virtual UniqueFooBar UniqueFooBar {get; set;}
}
class UniqueFooBarConfiguration : EntityTypeConfiguration<UniqueFooBar> {
    public UniqueFooBarConfiguration() {
        // Define the tablename and schema
        Map(entity => entity.ToTable("UniqueFooBars"));

        //// Define non-conventional key
        HasKey(fooBar => fooBar.FooName);

        // Define FKs -  1-to-1
        HasRequired(fooBar => fooBar.Foo)
            .WithRequiredPrincipal(foo => foo.UniqueFooBar)
            .Map(key => key.MapKey("FooName"));
        HasRequired(fooBar => fooBar.Bar)
            .WithRequiredPrincipal(bar => bar.UniqueFooBar)
            .Map(key => key.MapKey("BarId"));
        // --------------------------------

    }

}
UniqueFooBar
是如下查找:

public class UniqueFooBar {

   public string FooName {get; set;}
   public int BarId {get; set;}

   // nav properties
   public virtual Foo Foo {get; set;}
   public virtual Bar Bar {get; set;}

}
限制条件如下:

  • Foo
    是唯一的
  • Foo
    Bar
  • Foo
    Name是主键

    fluent API如下所示:

    public class Bar {
    
       public int Id {get; set;}
    
       // inverse nav property
       public virtual UniqueFooBar UniqueFooBar {get; set;}
    
    }
    
    public class Foo {
    
       public string Name {get; set;}
    
       // inverse nav property
       public virtual UniqueFooBar UniqueFooBar {get; set;}
    }
    
    class UniqueFooBarConfiguration : EntityTypeConfiguration<UniqueFooBar> {
        public UniqueFooBarConfiguration() {
            // Define the tablename and schema
            Map(entity => entity.ToTable("UniqueFooBars"));
    
            //// Define non-conventional key
            HasKey(fooBar => fooBar.FooName);
    
            // Define FKs -  1-to-1
            HasRequired(fooBar => fooBar.Foo)
                .WithRequiredPrincipal(foo => foo.UniqueFooBar)
                .Map(key => key.MapKey("FooName"));
            HasRequired(fooBar => fooBar.Bar)
                .WithRequiredPrincipal(bar => bar.UniqueFooBar)
                .Map(key => key.MapKey("BarId"));
            // --------------------------------
    
        }
    
    }
    
    类UniqueFooBarConfiguration:EntityTypeConfiguration{
    公共UniqueFooBarConfiguration(){
    //定义表名和模式
    Map(entity=>entity.ToTable(“UniqueFooBars”);
    ////定义非常规密钥
    HasKey(fooBar=>fooBar.FooName);
    //定义FKs-1对1
    HasRequired(fooBar=>fooBar.Foo)
    .WithRequiredPrincipal(foo=>foo.UniqueFooBar)
    .Map(key=>key.MapKey(“FooName”);
    HasRequired(fooBar=>fooBar.Bar)
    .WithRequiredPrincipal(bar=>bar.UniqueFooBar)
    .Map(key=>key.MapKey(“BarId”);
    // --------------------------------
    }
    }
    
发生的情况是,
FooName
被添加到Foo表,而
BarId
被添加到Bar表

如果在
UniqueFooBar
的fluent API配置中,我尝试使用Foo的“Name”属性,则该字段已经存在,这是一个错误。如果我尝试使用Bar的“Id”属性,也会发生同样的情况

如何将UniqueFooBar配置为将FKs与
Foo.Name
Bar.Id
作为一对一关系

更新

  • Foo
    Bar
    都没有
    UniqueFooBar
    的约束或要求
  • UniqueFooBar记录需要
    FooName
    BarId
这似乎与取自的不同,下面是一个示例,说明如何在两个实体之间实现一对一映射,对链接表进行外推,根据需要添加HasRequired

可以在不使用lambda的情况下指定WithRequiredPrincipal,这允许您排除导航属性并仍然获得正确的一对一映射

在OnModelCreating方法的重写中,可以使用DBModelBuilder参数定义关系

public class Customer
{
    public Customer()
    {
        Address = new Address();
    }

    public Guid Id { get; set; }
    public string Name { get; set; }
    public Address Address { get; set; }
}

public class Address
{
    public Guid Id { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public string Street { get; set; }
}

public  class  CustomerContext : DbContext
{
    public IDbSet<Customer> Customers { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         modelBuilder.Entity<Customer>()
                .HasRequired(x => x.Address)
                .WithRequiredPrincipal();
         base.OnModelCreating(modelBuilder);
    }
}
公共类客户
{
公众客户()
{
地址=新地址();
}
公共Guid Id{get;set;}
公共字符串名称{get;set;}
公共广播地址{get;set;}
}
公共课堂演讲
{
公共Guid Id{get;set;}
公共字符串City{get;set;}
公共字符串国家{get;set;}
公共字符串Street{get;set;}
}
公共类CustomerContext:DbContext
{
公共IDbSet客户{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasRequired(x=>x.Address)
.WithRequiredPrincipal();
基于模型创建(modelBuilder);
}
}
下面是一个示例,说明了如何在两个实体之间实现一对一映射,将其外推到链接表中,并根据需要添加HasRequired

可以在不使用lambda的情况下指定WithRequiredPrincipal,这允许您排除导航属性并仍然获得正确的一对一映射

在OnModelCreating方法的重写中,可以使用DBModelBuilder参数定义关系

public class Customer
{
    public Customer()
    {
        Address = new Address();
    }

    public Guid Id { get; set; }
    public string Name { get; set; }
    public Address Address { get; set; }
}

public class Address
{
    public Guid Id { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public string Street { get; set; }
}

public  class  CustomerContext : DbContext
{
    public IDbSet<Customer> Customers { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         modelBuilder.Entity<Customer>()
                .HasRequired(x => x.Address)
                .WithRequiredPrincipal();
         base.OnModelCreating(modelBuilder);
    }
}
公共类客户
{
公众客户()
{
地址=新地址();
}
公共Guid Id{get;set;}
公共字符串名称{get;set;}
公共广播地址{get;set;}
}
公共课堂演讲
{
公共Guid Id{get;set;}
公共字符串City{get;set;}
公共字符串国家{get;set;}
公共字符串Street{get;set;}
}
公共类CustomerContext:DbContext
{
公共IDbSet客户{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasRequired(x=>x.Address)
.WithRequiredPrincipal();
基于模型创建(modelBuilder);
}
}

可能重复的内容你读过吗?@PaulZahra如果你能在接受的答案中总结链接,那么我就接受它:因为附带了赏金,复制相关的代码片段时可以做一点总结。可能重复的内容你读过吗?@PaulZahra如果你能在接受的答案中总结链接,然后我会接受它:因为附带了一份赏金,所以复制相关代码片段时可以做一点总结。谢谢您的额外解释。。。我想我明白为什么以前事情不顺利。如果我理解正确,因为我有一个前进导航属性,我不需要在
WithRequiredPrincipal
方法中提供lambda;这里似乎有些混乱。谢谢你的额外解释。。。我想我明白为什么以前事情不顺利。如果我理解正确,因为我有一个前进导航属性,我不需要在
WithRequiredPrincipal
方法中提供lambda;这似乎是一些混乱的地方。