Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.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
Entity framework 4 如何在Entity Frame 4.0 Code First CTP4中映射非主键的集合?_Entity Framework 4_Code First - Fatal编程技术网

Entity framework 4 如何在Entity Frame 4.0 Code First CTP4中映射非主键的集合?

Entity framework 4 如何在Entity Frame 4.0 Code First CTP4中映射非主键的集合?,entity-framework-4,code-first,Entity Framework 4,Code First,我在尝试映射现有遗留数据库时遇到问题。 在这个例子中,一个国家有许多城市 问题是City表上的外键不是Country表的主键 表格定义 CREATE TABLE [dbo].[CD_Country]( [Id] [int] IDENTITY(1,1) NOT NULL, [Code] [char](2) NOT NULL, [Name] [nvarchar](50) NOT NULL, CONSTRAINT [PK_CD_Country] PRIMARY KEY CLUSTERED (

我在尝试映射现有遗留数据库时遇到问题。 在这个例子中,一个国家有许多城市

问题是City表上的外键不是Country表的主键

表格定义

CREATE TABLE [dbo].[CD_Country](
 [Id] [int] IDENTITY(1,1) NOT NULL,
 [Code] [char](2) NOT NULL,
 [Name] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_CD_Country] PRIMARY KEY CLUSTERED 
(
 [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[CD_City](
 [Id] [int] IDENTITY(1,1) NOT NULL,
 [Code] [char](3) NOT NULL,
 [Name] [nvarchar](50) NOT NULL,
 [CountryCode] [char](2) NOT NULL,
 CONSTRAINT [PK_CD_City] PRIMARY KEY CLUSTERED 
(
 [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
我试着做这样的事情,但我得到了例外

“导航属性'Country'到联接表'CD_Country'的映射无效。“Test_SOLEF.City”类型的每个键属性必须在映射中仅包含一次。”

公共静态类测试
{
公共静态void DoIt()
{
使用(TestEFContext上下文=新TestEFContext())
{
foreach(context.Coutries.ToList()中的国家)
{
WriteLine(string.Format(“{0}有{1}个城市。”,country.Name,country.cities.Count));
}
}
}
}
公共类TestEFContext:DbContext
{
公共IDbSet语句{get;set;}
公共IDbSet城市{get;set;}
公共测试上下文()
{
ObjectContext.ContextOptions.LazyLoadingEnabled=true;
}
模型创建时受保护的覆盖无效(System.Data.Entity.ModelConfiguration.ModelBuilder ModelBuilder)
{
//城市
modelBuilder.Entity()
.HasKey(p=>p.Id)
.MapSingleType()
.ToTable(“CD_城”);
modelBuilder.Entity()
.Property(c=>c.Id).IsIdentity();
modelBuilder.Entity()
.has可选(c=>c.Country)
.有许多(c=>c.城市)
.地图(
“CD_国家”,
(城市、国家)=>新建
{
CountryCode=city.CountryCode,
代码=国家代码
});
//国家
modelBuilder.Entity()
.HasKey(p=>p.Id)
.MapSingleType()
.ToTable(“CD_国家”);
}
}
公营城市
{
公共int Id{get;set;}
公共字符串代码{get;set;}
公共字符串名称{get;set;}
公共字符串CountryCode{get;set;}
公共虚拟国家{get;set;}
}
公营国家
{
公共int Id{get;set;}
公共字符串代码{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection城市{get;set;}
}

微软的正式回复

“FKs到非主键是实体框架目前不支持的,但我们正在研究

谢谢你,亚瑟“

论坛帖子

public static class Test
{
    public static void DoIt()
    {
        using (TestEFContext context = new TestEFContext())
        {
            foreach (Country country in context.Coutries.ToList())
            {
                Console.WriteLine(string.Format("{0} has {1} cities.", country.Name, country.Cities.Count));
            }
        }
    }
}

public class TestEFContext : DbContext
{
    public IDbSet<Country> Coutries { get; set; }
    public IDbSet<City> Cities { get; set; }

    public TestEFContext()
    {
        ObjectContext.ContextOptions.LazyLoadingEnabled = true;
    }

    protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
    {
        // city
        modelBuilder.Entity<City>()
            .HasKey(p => p.Id)
            .MapSingleType()
            .ToTable("CD_City");

        modelBuilder.Entity<City>()
            .Property(c => c.Id).IsIdentity();

        modelBuilder.Entity<City>()
            .HasOptional(c => c.Country)
            .WithMany(c => c.Cities)
            .Map(
                "CD_Country",
                (city, country) => new 
                {
                    CountryCode = city.CountryCode,
                    Code = country.Code
                });

        // country
        modelBuilder.Entity<Country>()
            .HasKey(p => p.Id)
            .MapSingleType()
            .ToTable("CD_Country");

    }

}

public class City
{
    public int Id {get;set;}
    public string Code {get;set;}
    public string Name {get;set;}
    public string CountryCode {get;set;}

    public virtual Country Country {get;set;}
}

public class Country
{
    public int Id {get;set;}
    public string Code {get;set;}
    public string Name {get;set;}

    public virtual ICollection<City> Cities {get;set;}
}