I've followed a tutorial in MVC 3, and done this:
Made 4 classes and a DBClass:
User [has a] List of Websites [has a] List of Pages [has a] List of Comments [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.