我首先生成实体框架数据库(EF Designer from database)。我在Microsoft SQL Server中有两个表:
CREATE TABLE dbo.Person
(
Pk_Person_Id INT IDENTITY PRIMARY KEY,
Name VARCHAR(255),
EmailId VARCHAR(255),
);
CREATE TABLE dbo.PassportDetails
(
Pk_Passport_Id INT PRIMARY KEY,
Passport_Number VARCHAR(255),
Fk_Person_Id INT UNIQUE
FOREIGN KEY REFERENCES dbo.Person(Pk_Person_Id)
);
INSERT INTO dbo.Person
VALUES ('Niraj','v.a@emails.com'),
('Vishwanath','v.v@emails.com'),
('Chetan','c.v@emails.com');
GO
INSERT INTO dbo.PassportDetails
VALUES (101, 'C3031R33', 1), (102, 'VRDK5695', 2), (103, 'A4DEK33D', 3);
GO
SELECT * FROM dbo.Person
SELECT * FROM dbo.PassportDetails;
在SQL Server中,关系显示为一对一关系,因为
Fk_Person_Id INT UNIQUE
FOREIGN KEY REFERENCES dbo.Person(Pk_Person_Id));
现在在Visual Studio中,我添加了一个新的ADO.NET实体数据模型&>数据库中EF设计器-&>选中这两个表,默认情况下保留所有复选框选项。
生成结束后,我在Visual Studio关系图
而且这种关系已经改变为一对多--为什么?这有错吗?我不想让一个人拥有一大堆护照--这不是我想要描述的逻辑。
和EF代码:
public partial class Person
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Person()
{
this.PassportDetails = new HashSet<PassportDetail>();
}
public int Pk_Person_Id { get; set; }
public string Name { get; set; }
public string EmailId { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PassportDetail> PassportDetails { get; set; }
}
public partial class PassportDetail
{
public int Pk_Passport_Id { get; set; }
public string Passport_Number { get; set; }
public Nullable<int> Fk_Person_Id { get; set; }
public virtual Person Person { get; set; }
}
上下文
public partial class LightCRMEntities : DbContext
{
public LightCRMEntities() : base("name=LightCRMEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<PassportDetail> PassportDetails { get; set; }
public virtual DbSet<Person> People { get; set; }
}
在database model editor(code.edmx/code>file editor)中,通过单击一个关系(entities daigrams之间的箭头),您可以以这样的方式配置它的属性,使其成为1比1(它不会改变您的数据库),但是您可以生成创建这样的数据库所需的代码。结果,我得到了以下信息:
-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------
-- Creating table 'PassportDetails'
CREATE TABLE [dbo].[PassportDetails] (
[Pk_Passport_Id] int NOT NULL,
[Passport_Number] varchar(255) NULL
);
GO
-- Creating table 'Person'
CREATE TABLE [dbo].[Person] (
[Pk_Person_Id] int IDENTITY(1,1) NOT NULL,
[Name] varchar(255) NULL,
[EmailId] varchar(255) NULL
);
GO
-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------
-- Creating primary key on [Pk_Passport_Id] in table 'PassportDetails'
ALTER TABLE [dbo].[PassportDetails]
ADD CONSTRAINT [PK_PassportDetails]
PRIMARY KEY CLUSTERED ([Pk_Passport_Id] ASC);
GO
-- Creating primary key on [Pk_Person_Id] in table 'Person'
ALTER TABLE [dbo].[Person]
ADD CONSTRAINT [PK_Person]
PRIMARY KEY CLUSTERED ([Pk_Person_Id] ASC);
GO
-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------
-- Creating foreign key on [Pk_Passport_Id] in table 'PassportDetails'
ALTER TABLE [dbo].[PassportDetails]
ADD CONSTRAINT [FK__PassportD__Pk_Pa__5BE2A6F2]
FOREIGN KEY ([Pk_Passport_Id])
REFERENCES [dbo].[Person]
([Pk_Person_Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
GO