1.配置entitframework节点(一般安装EF时自动添加)
添加AccountUser类
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace EFCodeFirst.Entity
{
[Table("user")]
public class AccountUser
{
///
/// 用户ID
///
[Column("ID")]
[Key]
public int AccountUserId { get; set; }
///
/// 用户名
///
public string Name { get; set; }
///
/// 年龄
///
public Nullable Age { get; set; }
///
/// 性别
///
public Nullable Sex { get; set; }
}
}
注:特性“Table”指定数据库中与该实体产生映射的表名,默认不添加系统会去数据库中寻找表名为:“类名+s”的表,找不到会报错。
特性“Column”来指定映射表中的列名,如果属性名与列名相同,则不用添加,系统默认属性名与列名一致。
以上方式为Data Annotations模式(数据注解)映射数据库方法。
EF还提供Fluent API配置来映射数据库,下面会讲到。
using System.Data.Entity;
namespace EFCodeFirst
{
public class DContext : DbContext
{
///
/// 添加构造函数,name为config文件中数据库连接字符串的name
///
public DContext() : base("name=MyContext")
{
}
#region 数据集
public DbSet AccountUsers { get; set; }
#endregion
}
}
这里声明了与数据库映射的对象AccountUser,以后可以直接用属性AccountUsers来进行对数据库的操作
注:一定要添加构造函数并指明config文件中数据库连接字符串的name,否则系统将把数据库默认指定到VS自带的数据库中
class Program
{
static void Main(string[] args)
{
try
{
var user = new AccountUser()
{
Name = "Test",
Sex = true,
Age = 29
};
using (var context = new DContext())
{
context.AccountUsers.Add(user);
context.SaveChanges();
var accountUsers = context.AccountUsers.ToList();
}
Console.Write("{0}", user.AccountUserId);
}
catch (Exception ex)
{
Console.Write("{0}", ex);
}
Console.ReadLine();
}
}
1.添加新类省、市
public class Provice
{
///
/// 省份ID
///
public int ProviceId { get; set; }
///
/// 省份名
///
public string ProviceName { get; set; }
///
/// 省份下城市
///
public List Citys { get; set; }
}
public class City
{
///
/// 城市ID
///
public int CityId { get; set; }
///
/// 所属省份ID
///
public int ProviceId { get; set; }
///
/// 城市名称
///
public string CityName { get; set; }
///
/// 所属城市
///
public Provice ProviceData { get; set; }
}
using System.Data.Entity.ModelConfiguration;
namespace EFCodeFirst
{
public class ProviceConfiguration : EntityTypeConfiguration
{
public ProviceConfiguration()
{
ToTable("provice") //映射表
.HasKey(q => q.ProviceId) //指定主键
.HasMany(q => q.Citys).WithRequired(q => q.ProviceData).HasForeignKey(q => q.ProviceId); //配置一对多关系
}
}
}
注:由于这里添加了外键,则必须用WithRequired方法而不能用WithOption方法,否则报错
using System.Data.Entity.ModelConfiguration;
namespace EFCodeFirst
{
public class CityConfiguration : EntityTypeConfiguration
{
public CityConfiguration()
{
ToTable("city")
.HasKey(q => q.CityId)
.Property(q => q.ProviceId).IsRequired();
}
}
}
一对多关系只用在一端配置,provice处配置后不需要在city端再配置
public class DContext : DbContext
{
///
/// 添加构造函数,name为config文件中数据库连接字符串的name
///
public DContext() : base("name=MyContext")
{
}
#region 数据集
public DbSet AccountUsers { get; set; }
public DbSet Provices { get; set; }
public DbSet Citys { get; set; }
#endregion
#region Fluent API配置
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ProviceConfiguration());
modelBuilder.Configurations.Add(new CityConfiguration());
}
#endregion
}
static void Main(string[] args)
{
try
{
var provice = new Provice
{
ProviceName = "河南省",
Citys = new List()
{
new City() { CityName = "安阳"},
new City() { CityName = "郑州"},
new City() { CityName = "洛阳"},
}
};
using (var context = new DContext())
{
context.Provices.Add(provice);
context.SaveChanges();
var provices = context.Provices.Include("Citys").ToList();
}
Console.Write("{0}", provice.ProviceId);
}
catch (Exception ex)
{
Console.Write("{0}", ex);
}
Console.ReadLine();
}