日期:2014-05-20  浏览次数:20828 次

EF为什么会生成多余字段???
EF模型


用“根据模型生成数据库”生成的SQL脚本:
SQL code

CREATE TABLE [dbo].[User] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Blog_ID] int  NOT NULL
);
GO

-- Creating table 'Blog'
CREATE TABLE [dbo].[Blog] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [UserId] int  NOT NULL
);
GO

-- Creating table 'Post'
CREATE TABLE [dbo].[Post] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Title] nvarchar(max)  NOT NULL,
    [Content] nvarchar(max)  NOT NULL,
    [BlogId] int  NOT NULL,
    [Blog_ID] int  NOT NULL
);
GO

-- Creating table 'Comment'
CREATE TABLE [dbo].[Comment] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Title] nvarchar(max)  NOT NULL,
    [Content] nvarchar(max)  NOT NULL,
    [PostId] int  NOT NULL,
    [Post_ID] int  NOT NULL
);
GO



大家看到了吧,为什么会给每张表生成多余一个外键字段呢(Blog_ID,Post_ID,这些加了下滑线的)?
这些外键字段我在模型时都已经弄好了的。

------解决方案--------------------
C# code

[ForeignKey("tid")]
        public AdDisplayType DisplayType
        {
            get;
            set;
        }

        public int tid { get; set; }

------解决方案--------------------
你还记得你建立这个东西的时候,那个向导页不, 下面有小的选项框“在模型中加入外键列”,你勾选的他,自然EF就生成了他,这是你自己选择滴。咋又怪到EF头上了