I want to create a SQL Server database in .net using Entity Framework, but I can't find how to do it.
As of now I have:
- A simple project (WCF service) with all the default name
A database model
A SQL file which should create the database; I generated it using the model (generate database from model)
A service file (
.svc
)
Code:
public class Service1 : IService1
{
public Entity1 GetData(int id)
{
using (Model1Container ctx = new Model1Container()) {
try {
Entity1 entity = ctx.Entity1Set.FirstOrDefault(e => e.Id == id);
return entity;
}
catch (Exception e) {
Console.WriteLine("CAPTAIN, WE GOT AN ERROR: " + e);
return null;
}
}
}
public String PostData(string value)
{
using (Model1Container ctx = new Model1Container()){
Entity1 newEntity = new Entity1();
newEntity.Property1 = value;
ctx.Entity1Set.Add(newEntity);
try {
ctx.SaveChanges();
} catch (Exception e) {
return ("CAPTAIN, WE GOT AN ERROR: " + e);
}
}
return "0";
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
But the problem is that is seems that the database isn't created. I cannot find how to run the SQL file, the server explorer shows nothing, and when I try to do a request in my database I got the following error:
System.Data.SqlClient.SqlException: Invalid object name 'dbo.Entity1Set'.
Did I forgot something important?