gpt4 book ai didi

asp.net-mvc - Entity Framework Code First 数据库文件未在 APP_DATA 中创建,但查询有效

转载 作者:行者123 更新时间:2023-12-05 01:29:35 25 4
gpt4 key购买 nike

我的博客模型:

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; }
}
}

这是我的数据库集:

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 中运行没有连接字符串的应用程序,它运行良好,我在 Controller 中看到我的所有标签,但 APP_DATA 中没有数据库文件??怎么会这样?或者发生了什么?

最佳答案

如果您不提供连接字符串,EF Code First 会在本地 SQLExpress 实例上使用用户凭据 (windows) 创建数据库。您可以使用 SQL Server Management Studio Express 并登录到本地实例来进行验证。

关于asp.net-mvc - Entity Framework Code First 数据库文件未在 APP_DATA 中创建,但查询有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9208577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com