Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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
Sql server 2008 代码首先生成奇怪的列_Sql Server 2008_Asp.net Mvc 4_Ef Code First_Code First_Entity Framework Migrations - Fatal编程技术网

Sql server 2008 代码首先生成奇怪的列

Sql server 2008 代码首先生成奇怪的列,sql-server-2008,asp.net-mvc-4,ef-code-first,code-first,entity-framework-migrations,Sql Server 2008,Asp.net Mvc 4,Ef Code First,Code First,Entity Framework Migrations,我有一个MVC4应用程序,它首先使用代码在SQLServerDB中生成表和列。我正试图弄清楚我是如何得到一张原本不需要的额外表格的。我看过一些问题,但没有发现完全相同的问题,我有。我会尽量简单地解释这一点 我添加了一个名为Associate的模型,它跟踪我的客户与之做生意的同事。每个员工都需要一个AssociateTypedID和RegionID的外键 namespace XXX.Models { public class Associate {

我有一个MVC4应用程序,它首先使用代码在SQLServerDB中生成表和列。我正试图弄清楚我是如何得到一张原本不需要的额外表格的。我看过一些问题,但没有发现完全相同的问题,我有。我会尽量简单地解释这一点

我添加了一个名为Associate的模型,它跟踪我的客户与之做生意的同事。每个员工都需要一个AssociateTypedID和RegionID的外键

    namespace XXX.Models
    {
        public class Associate
        {
            public int AssociateId { get; set; }
            public string AssociateName { get; set; }
            public int AddressNumber { get; set; }
            public string AddressStreet { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Zipcode { get; set; }
            public string MainPhoneNumber { get; set; }
            public string AssociateEmail { get; set; }
            public string AssociateWebsite { get; set; }
            public string ContactFirstName { get; set; }
            public string ContactLastName { get; set; }
            public string ContactPhoneNumber { get; set; }
            public string ContactEmail { get; set; }
            public int RegionId { get; set; }
            public int AssociateTypeId { get; set; }

            public virtual ICollection<AssociateType> AssociateTypes { get; set; }
            public virtual ICollection<Region> Regions { get; set; }
        }
    }
namespace XXX.Models
{
公共班级助理
{
public int AssociateId{get;set;}
公共字符串AssociateName{get;set;}
public int AddressNumber{get;set;}
公共字符串AddressStreet{get;set;}
公共字符串City{get;set;}
公共字符串状态{get;set;}
公共字符串Zipcode{get;set;}
公共字符串MainPhoneNumber{get;set;}
公共字符串AssociateMail{get;set;}
公共字符串关联网站{get;set;}
公共字符串ContactFirstName{get;set;}
公共字符串ContactLastName{get;set;}
公共字符串ContactPhoneNumber{get;set;}
公共字符串ContactEmail{get;set;}
public int RegionId{get;set;}
public int AssociateTypeId{get;set;}
公共虚拟ICollection关联类型{get;set;}
公共虚拟ICollection区域{get;set;}
}
}

namespace XXX.Models
{
公共类关联类型
{
public int AssociateTypeId{get;set;}
公共字符串AssociateTypeName{get;set;}
公共虚拟ICollection关联{get;set;}
}
}

namespace XXX.Models
{
公共类区域
{
public int RegionId{get;set;}
public int RegionName{get;set;}
public int RegionDescription{get;set;}
公共虚拟ICollection关联{get;set;}
}
}

namespace XXX.Models
{
公共类XXXDb:DbContext
{
public XXXDb():base(“name=DefaultConnection”)
{ 
}
公共数据库集关联{get;set;}
公共数据库集关联类型{get;set;}
公共数据库集区域{get;set;}
}
}
因此,我已经更新了上面的代码,我已经非常接近我需要在数据库中的位置。我生成了以下表格

AssociateTypes&Regions(每个都有我期望的列)

但我现在有一个名为RegionAssociates的新表,它有以下列:

区域\区域ID(内部)和关联\关联ID(内部)


此表在我的架构中不是预期的或需要的。

无法确定您的配置中缺少了什么,因为您没有发布它,但是如果您使用的是fluent api,类似这样的内容应该可以解决问题:

modelBuilder.Entity<AssociateType>()
.HasKey(t => t.AssociateTypeId);

modelBuilder.Entity<Associate>()
.HasRequired(t => t.AssociateType)
.WithRequiredPrincipal(t => t.Associate);

modelBuilder.Entity

您的类与您对模型的描述不匹配。你是说

每个员工都可以指定为AssociateType

我假设相同的
AssociateType
可以分配给更多的
AssociateType
s,因此
AssociateType
AssociateType
之间应该有1:N的关系

但是,
AssociateType
类以另一种方式定义了关系——按照约定,
public-virtualicollection-AssociateType{get;set;}
AssociateType
AssociateType
之间创建1:N关系

您的类的正确定义是

public class Associate
    {
        public int AssociateId { get; set; }
        public string AssociateName { get; set; }
        public int AddressNumber { get; set; }
        public string AddressStreet { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zipcode { get; set; }
        public string MainPhoneNumber { get; set; }
        public string AssociateEmail { get; set; }
        public string AssociateWebsite { get; set; }
        public int RegionId { get; set; }
        public int AssociateTypeId { get; set; }
        public virtual AssociateType AssociateType { get; set; }
        public string ContactFirstName { get; set; }
        public string ContactLastName { get; set; }
        public string ContactPhoneNumber { get; set; }
        public string ContactEmail { get; set; }

        public virtual ICollection<Region> Regions { get; set; }
    }

public class AssociateType
    {
        public int AssociateTypeId { get; set; }
        public string AssociateTypeName { get; set; }

        public virtual ICollection<Associate> Associates { get; set; }
    }
公共类助理
{
public int AssociateId{get;set;}
公共字符串AssociateName{get;set;}
public int AddressNumber{get;set;}
公共字符串AddressStreet{get;set;}
公共字符串City{get;set;}
公共字符串状态{get;set;}
公共字符串Zipcode{get;set;}
公共字符串MainPhoneNumber{get;set;}
公共字符串AssociateMail{get;set;}
公共字符串关联网站{get;set;}
public int RegionId{get;set;}
public int AssociateTypeId{get;set;}
公共虚拟AssociateType AssociateType{get;set;}
公共字符串ContactFirstName{get;set;}
公共字符串ContactLastName{get;set;}
公共字符串ContactPhoneNumber{get;set;}
公共字符串ContactEmail{get;set;}
公共虚拟ICollection区域{get;set;}
}
公共类关联类型
{
public int AssociateTypeId{get;set;}
公共字符串AssociateTypeName{get;set;}
公共虚拟ICollection关联{get;set;}
}

好的,我现在明白了,回顾一下我的另一款没有在这里展示的车型,我在哪里做的正确,我看到了我做错了什么。只需多读一点关于ICollection的内容。昨晚工作了很长时间,我只是不完全明白我在做什么。谢谢你的帮助!好的,我运行了一个更新数据库迁移命令,它删除了我的AssociateType表。那不应该发生?不,这不应该发生。您是否已将此属性
public virtual AssociateType AssociateType{get;set;}
添加到
Associate
类中?外键旁边必须有一个导航属性
AssociateTypeId
,使其在没有配置的情况下工作。另一个选项是使用fluent配置。让我们假设每个关联可以有一个由相应的AssociateTypeID表示的AssociateType列。每个员工还可以有一个RegionID表示的Region列,我正在将我的原始代码更新为s
    namespace XXX.Models
    {
        public class XXXDb : DbContext
        {
            public XXXDb(): base("name=DefaultConnection")
            { 

            }
            public DbSet<Associate> Associates { get; set; }
            public DbSet<AssociateType> AssociateTypes { get; set; }
            public DbSet<Region> Regions { get; set; }
        }
    }
modelBuilder.Entity<AssociateType>()
.HasKey(t => t.AssociateTypeId);

modelBuilder.Entity<Associate>()
.HasRequired(t => t.AssociateType)
.WithRequiredPrincipal(t => t.Associate);
public class Associate
    {
        public int AssociateId { get; set; }
        public string AssociateName { get; set; }
        public int AddressNumber { get; set; }
        public string AddressStreet { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zipcode { get; set; }
        public string MainPhoneNumber { get; set; }
        public string AssociateEmail { get; set; }
        public string AssociateWebsite { get; set; }
        public int RegionId { get; set; }
        public int AssociateTypeId { get; set; }
        public virtual AssociateType AssociateType { get; set; }
        public string ContactFirstName { get; set; }
        public string ContactLastName { get; set; }
        public string ContactPhoneNumber { get; set; }
        public string ContactEmail { get; set; }

        public virtual ICollection<Region> Regions { get; set; }
    }

public class AssociateType
    {
        public int AssociateTypeId { get; set; }
        public string AssociateTypeName { get; set; }

        public virtual ICollection<Associate> Associates { get; set; }
    }