实体框架引发异常 - 发生网络相关或特定于实例的错误
我遵循了 MVC 3 中的教程,并完成了此操作:
制作了 4 个类和一个 DBClass:用户
[有] 网站
列表 [有] 页面
列表 [有] 评论s [每个标记的单词都是它自己的一个类]。
然后,我这样创建了一个名为 UserDataBaseDB
的类:
public partial class UserDataBaseDB : DbContext
{
public DbSet<User> Users { get; set; }
public UserDataBaseDB()
: base(@"UserDataBaseDB")
{
if (this.Database.Exists())
{
try
{
this.Database.CompatibleWithModel(false);
}
catch (InvalidOperationException)
{
this.Database.Delete();
}
}
if (!this.Database.Exists())
{
this.Database.Create();
Seed();
}
}
private void Seed()
{
var Mark = new User();
Mark.ID = 0;
Mark.MD5Pass = "4297f44b13955235245b2497399d7a93";
Mark.UserName = "Admin";
var Troll = new Comment();
Troll.CommentData = "Fake!";
Troll.Writer = Mark;
var Site = new Website();
Site.Name = "Admin Site";
Site.Pages.ElementAt(0).Data = "Awesome website!";
Site.Pages.ElementAt(0).Comments.Add(Troll);
Mark.Websites.Add(Site);
Users.Add(Mark);
}
public static UserDataBaseDB Create()
{
return new UserDataBaseDB();
}
public User FindUserByID(int id)
{
return (from item in this.Users
where item.ID == id
select item).SingleOrDefault();
}
然后我添加了 Controller
和 查看
,以及导航到 http://localhost:40636/Main
,我看到了这条消息:
建立与 SQL 的连接时发生网络相关或特定于实例的错误服务器。找不到服务器或无法访问服务器。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:SQL 网络接口,错误:26 - 定位指定的服务器/实例时出错)
Line 14: : base(@"UserDataBaseDB")
Line 15: {
Line 16: **if (this.Database.Exists())**
Line 17: {
Line 18: try
让标记线成为红线。
我尝试重新-安装实体框架,也尝试 EFCodeFirst,尝试制作自己的 SqlDataBase
并为其提供连接字符串,但没有任何效果。
有谁知道解决方案是什么?谢谢!
PS:我在Win7 64位机器上使用VS Ultimate SP1。
I've followed a tutorial in MVC 3, and done this:
Made 4 classes and a DBClass:User
[has a] List of Website
s [has a] List of Page
s [has a] List of Comment
s [each marked word is a class of its own].
Then, I made a class called UserDataBaseDB
this way:
public partial class UserDataBaseDB : DbContext
{
public DbSet<User> Users { get; set; }
public UserDataBaseDB()
: base(@"UserDataBaseDB")
{
if (this.Database.Exists())
{
try
{
this.Database.CompatibleWithModel(false);
}
catch (InvalidOperationException)
{
this.Database.Delete();
}
}
if (!this.Database.Exists())
{
this.Database.Create();
Seed();
}
}
private void Seed()
{
var Mark = new User();
Mark.ID = 0;
Mark.MD5Pass = "4297f44b13955235245b2497399d7a93";
Mark.UserName = "Admin";
var Troll = new Comment();
Troll.CommentData = "Fake!";
Troll.Writer = Mark;
var Site = new Website();
Site.Name = "Admin Site";
Site.Pages.ElementAt(0).Data = "Awesome website!";
Site.Pages.ElementAt(0).Comments.Add(Troll);
Mark.Websites.Add(Site);
Users.Add(Mark);
}
public static UserDataBaseDB Create()
{
return new UserDataBaseDB();
}
public User FindUserByID(int id)
{
return (from item in this.Users
where item.ID == id
select item).SingleOrDefault();
}
Then I added the Controller
and View
, and navigated to http://localhost:40636/Main
, and I saw this message:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Line 14: : base(@"UserDataBaseDB")
Line 15: {
Line 16: **if (this.Database.Exists())**
Line 17: {
Line 18: try
Letting the marked line be the red line.
I've tried to re-install Entity Framework, also trying EFCodeFirst, tried making my own SqlDataBase
and giving it the connection string, but nothing worked.
Does anyone know what's the solution? Thank you!
PS: I'm using VS Ultimate SP1 on a Win7 64bit machine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是一个连接字符串问题,但是我确实看到您正在尝试播种信息并充分使用构造函数中的上下文,我强烈建议您不要这样做。
您可以实现 OnModelCreating 方法并在那里尝试初始化代码,或者在应用程序启动时更好。停留在构造函数中似乎并不正确,因为此时初始化才刚刚开始。
设置断点
错误是否立即发生在该行之后?如果是这样,请三次检查您的连接字符串。您的代码是否在一个项目中,或者是分解的?如果您碰巧使用该设计,请确保您正在检查根 Web 项目的 web.config 中的连接字符串,而不是任何组件的 app.config 中的连接字符串。
每次您从代码优先等更改时,您是否更改了连接字符串?代码优先的连接字符串以您的上下文类命名,并且很简单:
当然是使用 SQLCe4。 Sql Server 将是
或用于 app_Data 中的数据库
This is likely a connect string issue, however I do see that you are trying to seed information and fully use the context in the constructor, which I would highly recommend you dont do.
You can implement the OnModelCreating method and attempt your initialization code there, or even better in your application start. It just doesnt seem right to be stuck in the constructor as initialization is just starting at this point.
Set a breakpoint on
Does the error happen immediately after that line? If so, triple check your connect string. Do you have your code in one project, or broken out? Make sure you are checking the connect string in your root web project's web.config and not any component's app.config if you so happen to be using that design.
Each time you changes from code-first, etc did you change your connect string? A code-first connect string is named after your context class and is simple:
That is of course using SQLCe4. Sql Server would be
or for a db in app_Data