Just getting started learning EF and databases in general, so I've been following this short tutorial: http://www.entityframeworktutorial.net/code-first/simple-code-first-example.aspx
I made it work with his example, but when I try with my own classes and such, no database is created (or at least I can't find it anywhere in the object explorer), and I hope you could tell me why?
I know that I have not declared a connection string in the base() part of the context class, but in the example from the tutorial he doesn't either, so that shouldn't be the problem I guess? Thanks in advance :)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShopEntity
{
class Product
{
public int ProductId { get; set; }
public int Price { get; set; }
public string ProductName { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ShopEntity
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
using (var ctx = new ShopContext())
{
User adminUser = new User()
{
Address = "Skejby Vænge 96A",
EMailAddress = "[email protected]",
IsAdmin = 1,
PassWord = "12345678",
UserName = "Anders1234"
};
//Product Apple = new Product() { Price = 2, ProductName = "Apple" };
//Product Melon = new Product() { Price = 10, ProductName = "Melon" };
//Product Beef = new Product() { Price = 40, ProductName = "Beef" };
ctx.Users.Add(adminUser);
//ctx.Products.Add(Apple);
//ctx.Products.Add(Melon);
//ctx.Products.Add(Beef);
ctx.SaveChanges();
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShopEntity
{
class ShopContext: DbContext
{
public ShopContext() : base()
{
}
public DbSet<User> Users { get; set; }
public DbSet<Product> Products { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShopEntity
{
class User
{
public int UserId { get; set; }
[MaxLength(20)]
public string UserName { get; set; }
[MaxLength(20)]
public string PassWord { get; set; }
public string EMailAddress { get; set; }
[MaxLength(50)]
public string Address { get; set; }
public byte IsAdmin { get; set; }
}
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
app.config?