我博客的模型:
namespace AlexPeta_2.Models
{
public class Article
{
public int ArticleId { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public char Published { get; set; }
public DateTime CreatedDate { get; set; }
public char AllowComment { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
}
namespace AlexPeta_2.Models
{
public class Comment
{
public int CommentId { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string HomePage { get; set; }
public string Comm { get; set; }
public DateTime CreatedDate { get; set; }
}
}
namespace AlexPeta_2.Models
{
public class Tag
{
public int TagId { get; set; }
public string TagName { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
}
这是我的dbsets:
public class BlogContext : DbContext
{
public DbSet<Article> Articles { get; set; }
public DbSet<Tag> Tags { get; set; }
public DbSet<Comment> Comments { get; set; }
}
这是我的初始化程序:
public class BlogInitializer : DropCreateDatabaseIfModelChanges<BlogContext>
{
protected override void Seed(BlogContext context)
{
List<Tag> tags = new List<Tag>
{
new Tag { TagId = 1, TagName = "Javascript" },
new Tag { TagId = 2, TagName = "Bodybuilding" },
new Tag { TagId = 3, TagName = "Uncategorised" },
new Tag { TagId = 4, TagName = "CSS" },
new Tag { TagId = 5, TagName = "HTML/XHTML" },
new Tag { TagId = 6, TagName = "APEX" },
new Tag { TagId = 7, TagName = "PL/SQL" },
new Tag { TagId = 8, TagName = "Personal" },
new Tag { TagId = 9, TagName = "ASP" },
new Tag { TagId = 10, TagName = "MVC" },
new Tag { TagId = 11, TagName = "C#" },
new Tag { TagId = 12, TagName = "Snowboarding" }
};
tags.ForEach(t => context.Tags.Add(t));
context.SaveChanges();
}
}
有趣的是:我在webconfig中没有连接字符串运行应用程序,它运行正常,我在控制器中看到我的所有标签,但APP_DATA中没有数据库文件?那怎么样?或者发生了什么?
答案 0 :(得分:5)
如果未提供连接字符串,则EF Code First将在本地SQLExpress实例上使用用户凭据(窗口)创建数据库。您可以使用SQL Server Management Studio Express并登录到本地实例来验证相同的内容。
答案 1 :(得分:2)
亚历,
connectionstring实际上是从dbcontext(在你的情况下是 BlogContext )中自动派生的(即通过约定),所以你会发现在sqlexpress或sqlcompact中你将拥有一个新的db使用dbcontext具有的名称。
祝你好运。