双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

Code First基础问题

手抓宝 手抓宝 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
小弟刚接触Code First,之前都是Database First的。。有个问题不太明白
写好了实体,写好了DBContext

public class TestTableContext : DbContext
{
public TestTableContext(string databaseName):base(databaseName){}

public DbSet<TestTable> TestTables { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//base.OnModelCreating(modelBuilder);
modelBuilder.Entity<TestTable>().ToTable("Table1");
//modelBuilder.Configurations.Add(new TestTableMap());
}
}

这样是不是就可以了呢?
然后我在程序里边用的时候:

static void Main(string[] args)
{
TestTable tt = new TestTable() { Name = "haha", age = 25 };

using (var context = new TestTableContext("ContextString"))
{
/*这里有问题
/*这里如果放开注释插入数据,会在SQLServer中新建一个数据库和一张表,并且插入数据,一切正常
/*这里如果注释掉,就是不插入数据,就不会创建数据库和表。*/
//context.TestTables.Add(tt);
context.SaveChanges();
}

Console.ReadKey();
}


想问下Code First就是这样的嘛?还是我哪儿少写什么东西了?
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
手抓宝
.NET社区 62,274 成员 668,968 内容