gerade erst begonnen EF und Datenbanken Lernen im Allgemeinen, also habe ich diese kurze Anleitung verfolgt: http://www.entityframeworktutorial.net/code-first/simple-code-first-example.aspxDatenbank nicht erstellt werden, wenn Projekt gestartet wird
Ich machte es mit seinem Beispiel arbeiten, aber wenn ich versuche, mit meinem eigene Klassen und so, es wird keine Datenbank erstellt (oder zumindest finde ich sie nirgendwo im Objekt-Explorer), und ich hoffe, du könntest mir sagen, warum?
Ich weiß, dass ich keine Verbindungszeichenfolge im base() - Teil der Kontextklasse deklariert habe, aber im Beispiel aus dem Tutorial tut er das auch nicht, also sollte das nicht das Problem sein, denke ich? Vielen Dank im Voraus :)
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>
Ist Ihre Verbindungszeichenfolge in Ihrer 'app.config' definiert? –
Zwei Sekunden, werde die app.config zum Beitrag hinzufügen - sollte jetzt da sein :) – sunero4
siehe meine Antwort .. es gibt ein paar Dinge, die Sie tun müssen –