- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
好的2个问题:1. 我是数据库的新手,我想学习代码优先的方法来创建数据库。我已经多次使用模型优先方法。任何人都可以帮助我使用代码优先方法对这个数据库进行编码吗(如果可能的话,我也想添加一个 Employee 表)?这是数据库图的链接:
预先感谢任何愿意提供帮助的人。
最佳答案
你可以像下面那样做:
请注意此解决方案是作为控制台应用程序完成的。
请首先添加以下类作为代码执行此操作:
public class Customer
{
[Key]
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string CustomerOtherDetails { get; set; }
}
public class CustomerAddress
{
[ForeignKey("Customer")]
public int CustomerId { get; set; }
[ForeignKey("AddressType")]
public int AddressTypeId { get; set; }
[ForeignKey("Address")]
public int AddressId { get; set; }
[Key]
public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; }
public virtual Customer Customer { get; set; }
public virtual AddressType AddressType { get; set; }
public virtual Address Address { get; set; }
}
public class AddressType
{
[Key]
public int AddressTypeId { get; set; }
public string AddressTypeDescriptiom { get; set; }
}
public class Address
{
[Key]
public int AddressId { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
public string City { get; set; }
}
当你使用代码优先方法时,你需要从你创建的类中创建模型,它可以按如下方式完成:
上下文类应该如下所示:
public class CustomerContext : DbContext
{
public CustomerContext()
: base("DBConnectionString")
{
//If model change, It will re-create new database.
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<CustomerContext>());
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Set primary key to Customer table
modelBuilder.Entity<Customer>().HasKey(m => m.CustomerId);
//set primary key to Address table
modelBuilder.Entity<CustomerAddress>().HasKey(m => m.DateFrom);
modelBuilder.Entity<AddressType>().HasKey(m => m.AddressTypeId);
modelBuilder.Entity<Address>().HasKey(m => m.AddressId);
//Set foreign key property
modelBuilder.Entity<CustomerAddress>().HasRequired(t => t.Customer)
.WithMany().HasForeignKey(t => t.CustomerId);
modelBuilder.Entity<CustomerAddress>().HasRequired(t => t.AddressType)
.WithMany().HasForeignKey(t => t.AddressTypeId);
modelBuilder.Entity<CustomerAddress>()
.HasRequired(t => t.Address)
.WithMany()
.HasForeignKey(t => t.AddressId);
}
数据库的创建和客户的插入地址应该是这样的:
static void Main(string[] args)
{
using (var ctx = new CustomerContext())
{
//ctx.Database.Create(); // This command can be used to create the database using the code first class
ctx.CustomerAddresses.Add(new CustomerAddress
{
AddressType = new AddressType
{
AddressTypeId = 1,
AddressTypeDescriptiom = "Test"
},
Customer = new Customer
{
CustomerId = 1,
FirstName = "Customer 1"
},
Address = new Address
{
Line1 = "Line 1",
City = "USA"
},
DateFrom = DateTime.Now,
DateTo = DateTime.Now
});
ctx.SaveChanges();
}
}
连接字符串应该如下所示:
<connectionStrings>
<add name="DBConnectionString"
connectionString="Data Source=(local);Initial Catalog=CustomerDB;Integrated Security=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
请注意以下注意上面的代码需要以下引用。
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
我试过了,效果如你所料。请让我知道您是否需要这方面的示例项目。
希望对你有帮助。
已编辑:
要先在代码中配置自引用,请执行以下操作:
public class Product
{
[Key]
public int ProductId { get; set; }
public int? ParentProductId { get; set; }
public virtual Product ParentProduct { get; set; }
}
在OnModelCreating
方法中添加以下代码行:
modelBuilder.Entity<Product>().HasKey(m => m.ProductId);
modelBuilder.Entity<Product>().
HasOptional(e => e.ParentProduct).
WithMany().
HasForeignKey(m => m.ParentProductId);
关于c# - Entity Framework : Help Creating Database using Code first Approach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31769547/
我有一个字符串,我想多次使用它来输出 configure --help .所以我尝试在 configure.ac 中做这样的事情: AC_ARG_ENABLE([foobar],
我无法使其正确匹配。它只打印“[help]”,但我希望它匹配括号内的字符。 想要: [help] help 代码: Pattern p = Pattern.compile("\\[(\
我刚刚将vim74下载并安装到我的linux机器上。我只是为用户安装在本地。当我进入vim并执行:help时,我得到了错误。 我尝试添加: let $VIM='home/myuser/vim74' l
当我打开一个 Xcode 项目时,快速帮助功能工作得很好,但是当打开一个不在打开的项目中的文件(例如 abc.swift )时,文档不会出现。 下面是一个例子: 最佳答案 安装版本Command Li
我有一个 python click CLI。当我将 --help 传递给任何命令时,它会打印一条我喜欢的帮助消息。我发现现在很多用户都在输入 mycli help foo 而不是 mycli foo
我一直在使用 less 进行前端开发,但最近几天我遇到了这个错误。 我正在使用 PhpStorm 的观察器将 less 文件编译为 css 文件。但是当我编辑 less 文件时,编译器将这一行添加到
当我在 python 解释器中键入 help('string') 时,我得到了有关字符串类的信息。在那里,upper() 被表示为一个函数。然而,我只能将其称为 "hi".upper() 而不是 up
我希望 help 选项的作用与 --help 相同。 旁注:我创建了一个程序,其命令行行为与 svn 或 hg 相同。我设法用子解析器做到了这一点。但是我想让事情保持一致。这就是为什么我希望 help
我是 Maven 的新手。你知道我做错了什么吗(见下文)? mvn -e clean:help. + Error stacktraces are turned on. [INFO] Scannin
help format-list输出格式列表的帮助。 format-list | help输出帮助(get-help)。 最佳答案 该|或“管道”运算符将一个操作的输出重定向到另一个。因此,在这种情况
尝试从脚本检索帮助会出现以下错误: Get-Help : Cannot find Help for topic ".\Process-Test.ps1". At line:1 char:9 + get
我从常规的discord.py更改为重写版本,并且我曾经有一个自定义的!help命令。 现在,每当我发布 !help 时,它都会显示此内容以及编码的帮助消息: No Category: help S
我开发跨平台桌面应用程序,最近我发现了在小部件上使用帮助文本的乐趣。然后用户在标题栏上有一个问号按钮,单击该按钮后,他可以单击窗口中的任何小部件并查看有关它的帮助文本。非常好。 因为我认为所有程序都应
Xcode 4.5.2 Mountain Lion,Mac 应用 我严格遵守文档。帮助文件夹及其子文件夹已添加到项目/Resources 文件夹并显示为蓝色。 如有必要,文件夹引用由 xcode 添加
emulator documentation说: -audio-in Use the specified audio-input backend. 它还说 -help- Print
最近在学习argparse模块,代码下方出现Argument错误 import argparse import sys class ExecuteShell(object): def crea
我正在阅读有关 Maven 的教程,应该提到我对此一无所知。我已经成功地完成了第一章和第二章,这是关于它的设置的。但是在第 3 章“Maven Pom”中我遇到了问题。在最后一段中,它说在你想要的任何
我注意到 Windows 有这个问题,但我运行的是 Ubuntu 13.10(带有 Bash),所以我不知道出了什么问题。 尝试设置我的副本集,我运行以下命令: sudo bash # Accout
很抱歉,我在 python 程序中遇到了一个尴尬的问题。首先我是这里的新手,我是一名中国学生。请原谅我糟糕的英语。我会尽力把问题表达清楚。 错误代码在这里: def __find__(
好的,我试图环顾四周并弄清楚如何做到这一点,但无法弄清楚。这就是我想要做的。 # Run script normally .\myscript.ps1 "blah" "yo" 这些应该做同样的事情 G
我是一名优秀的程序员,十分优秀!