W explorer serwer dodałem nazwe serwera (localDB)\MSQLLocalDB a jako nazwe bazy Dbfirst. Tworze kontroler
namespace Projekt_z_baz_danych_1._0._0.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public ActionResult AddCustomer(Customer customer)
{
if (!ModelState.IsValid)
return View("Index", customer);
else
{
CustomerContext db = new CustomerContext();
db.Customers.Add(customer);
db.SaveChanges();
return View("Index");
}
}
public ActionResult ShowCustomer(int customerID)
{
CustomerContext db = new CustomerContext();
var customer = db.Customers.Find(customerID);
return View(customer);
}
}
}
Tworze kontekst
namespace Projekt_z_baz_danych_1._0._0.DAL
{
public class CustomerContext : DbContext
{
public CustomerContext()
: base("CustomerConnecionString")
{
}
public DbSet<Customer> Customers { get; set; }
}
}
Dodatkowo tworze inizjalizer (który wywołuje w Global,asax) i model widoku
namespace Projekt_z_baz_danych_1._0._0.DAL
{
public class CustomerInitializer : DropCreateDatabaseAlways<CustomerContext>
{
protected override void Seed(CustomerContext context)
{
var customers = new List<Customer>
{
new Customer() { customerID = 1 ,name = "xxx", address = "ul. Meissnera 6/57 Krakow", status = "Gracz"},
new Customer() { customerID = 2, name = "yyy", address = "ul. Lipinskiego 4/7 Krakow", status = "Gracz"}
};
customers.ForEach(c => context.Customers.Add(c));
context.SaveChanges();
}
}
}
A mój co connectionStrings wygląda tak:
<connectionStrings>
<add name="CustomerConnecionString"
connectionString="Data Source=(localDB)\MSSQLLocalDB;Initial Catalog=Dbfirst;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Klasa Customer:
namespace Projekt_z_baz_danych_1._0._0.Models
{
public class Customer : IValidatableObject
{
[Key]
[ScaffoldColumn(false)]
public int customerID { get; set; }
[Display(Name = "Imie Nazwisko")]
public string name { get; set; }
[Display(Name = "Address")]
public string address { get; set; }
[Display(Name = "Status")]
public string status { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
throw new NotImplementedException();
}
}
}
Gdy zaglądam do explorera bazy jest ona pusta(gdy uruchamiam widok wyświetlają się błędy). Nie wiem czy źle się łączę z tą bazą czy źle wysyłam dane. Jest w stanie ktoś mi pomóc gdzie może leżeć problem?