Ale*_*eta 2 asp.net-mvc code-first ef-code-first entity-framework-4.1
我博客的模型:
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的dbsets:
public class BlogContext : DbContext
{
public DbSet<Article> Articles { get; set; }
public DbSet<Tag> Tags { get; set; }
public DbSet<Comment> Comments { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的初始化程序:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
有趣的是:我在webconfig中没有连接字符串运行应用程序,运行正常,我在控制器中看到我的所有标签,但APP_DATA中没有数据库文件?那怎么样?或者发生了什么?
如果未提供连接字符串,则EF Code First将在本地SQLExpress实例上使用用户凭据(窗口)创建数据库.您可以使用SQL Server Management Studio Express并登录到本地实例来验证相同的内容.
| 归档时间: |
|
| 查看次数: |
1932 次 |
| 最近记录: |