Entity Framework mit lokaler SQL Datenbankdatei

  • C#
  • .NET (FX) 4.5–4.8

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von MichaHo.

    Entity Framework mit lokaler SQL Datenbankdatei

    Hi,
    ich versuche mich gerade in das Thema Entity Framework einzuarbeiten (6.1.3).
    Dazu habe ich mir eine kleine Anwendung erstellt. Jedoch hab ich scheinbar ein Problem die Verwendung einer lokalen SQLServer Datenbankdatei (*.mdf) zu verstehen.
    Hier mal der Aufbau.
    Projektmappe
    Klassenprojekt (Bazaar.Core) Ordner Models
    Seller.cs

    C#-Quellcode

    1. using System;
    2. using System.Collections.Generic;
    3. namespace Bazaar.Core.Models
    4. {
    5. public class Seller
    6. {
    7. public int ID { get; set; }
    8. public int SellerNo { get; set; }
    9. public string Name { get; set; }
    10. public decimal TurnOver { get; set; }
    11. public virtual ICollection<Article> Article { get; set; }
    12. public int CheckOutID { get; set; }
    13. public DateTime CheckOutDate { get; set; }
    14. public Seller()
    15. {
    16. Article = new HashSet<Article>();
    17. }
    18. }
    19. }


    Article.cs

    C#-Quellcode

    1. namespace Bazaar.Core.Models
    2. {
    3. public class Article
    4. {
    5. public int ID { get; set; }
    6. public int ArticleNo { get; set; }
    7. public string Matchcode { get; set; }
    8. public string Size { get; set; }
    9. public decimal Price { get; set; }
    10. public bool Sold { get; set; }
    11. public int SellerID { get; set; }
    12. public string SellerName { get; set; }
    13. }
    14. }


    CheckOut.cs

    C#-Quellcode

    1. using System;
    2. using System.Collections.Generic;
    3. namespace Bazaar.Core.Models
    4. {
    5. public class CheckOut
    6. {
    7. public int ID { get; set; }
    8. public DateTime BazaarDate { get; set; }
    9. public virtual ICollection<Seller> Seller { get; set; }
    10. public CheckOut()
    11. {
    12. Seller = new HashSet<Seller>();
    13. }
    14. }
    15. }


    Bazaar.Db

    C#-Quellcode

    1. namespace Bazaar.Core.Models
    2. {
    3. using System.Data.Entity;
    4. public partial class BazaarDb : DbContext
    5. {
    6. public BazaarDb()
    7. : base("name=BazaarDb")
    8. {
    9. }
    10. public virtual DbSet<Seller> Sellers { get; set; }
    11. public virtual DbSet<Article> Articles { get; set; }
    12. public virtual DbSet<CheckOut> CheckOuts { get; set; }
    13. }
    14. }

    Ich habe dann eine Lokale SQLDatenbankdatei angelegt und ein Leeres EF Model hinzugefügt.
    Die App.Config hat folgenden Connection String:

    XML-Quellcode

    1. <connectionStrings>
    2. <add name="BazaarDb" connectionString="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\BazaarDb.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
    3. </connectionStrings>

    Danach habe ich die Migrations enabled und eine erste Migration angelegt:
    InitialCreate

    C#-Quellcode

    1. namespace Bazaar.Core.Migrations
    2. {
    3. using System;
    4. using System.Data.Entity.Migrations;
    5. public partial class InitialCreate : DbMigration
    6. {
    7. public override void Up()
    8. {
    9. CreateTable(
    10. "dbo.Articles",
    11. c => new
    12. {
    13. ID = c.Int(nullable: false, identity: true),
    14. ArticleNo = c.Int(nullable: false),
    15. Matchcode = c.String(),
    16. Size = c.String(),
    17. Price = c.Decimal(nullable: false, precision: 18, scale: 2),
    18. Sold = c.Boolean(nullable: false),
    19. SellerID = c.Int(nullable: false),
    20. SellerName = c.String(),
    21. })
    22. .PrimaryKey(t => t.ID)
    23. .ForeignKey("dbo.Sellers", t => t.SellerID, cascadeDelete: true)
    24. .Index(t => t.SellerID);
    25. CreateTable(
    26. "dbo.CheckOuts",
    27. c => new
    28. {
    29. ID = c.Int(nullable: false, identity: true),
    30. BazaarDate = c.DateTime(nullable: false),
    31. })
    32. .PrimaryKey(t => t.ID);
    33. CreateTable(
    34. "dbo.Sellers",
    35. c => new
    36. {
    37. ID = c.Int(nullable: false, identity: true),
    38. SellerNo = c.Int(nullable: false),
    39. Name = c.String(),
    40. TurnOver = c.Decimal(nullable: false, precision: 18, scale: 2),
    41. CheckOutID = c.Int(nullable: false),
    42. CheckOutDate = c.DateTime(nullable: false),
    43. })
    44. .PrimaryKey(t => t.ID)
    45. .ForeignKey("dbo.CheckOuts", t => t.CheckOutID, cascadeDelete: true)
    46. .Index(t => t.CheckOutID);
    47. }
    48. public override void Down()
    49. {
    50. DropForeignKey("dbo.Sellers", "CheckOutID", "dbo.CheckOuts");
    51. DropForeignKey("dbo.Articles", "SellerID", "dbo.Sellers");
    52. DropIndex("dbo.Sellers", new[] { "CheckOutID" });
    53. DropIndex("dbo.Articles", new[] { "SellerID" });
    54. DropTable("dbo.Sellers");
    55. DropTable("dbo.CheckOuts");
    56. DropTable("dbo.Articles");
    57. }
    58. }
    59. }


    und dann ein Update-Database.
    Das scheint ohne Fehler durch zu laufen, allerdings sehe ich im ServerExplorer keine Veränderung. es wird keine Tabelle angelegt.
    Die Datenbankdatei steht auf "Immer kopieren".
    Ich hänge das Projekt mal mit an.
    Kann mir jemand kurz erklären, was ich falsch mache?
    Danke Euch
    Grüße
    Dateien
    • Bazaar00.zip

      (427,5 kB, 204 mal heruntergeladen, zuletzt: )
    "Hier könnte Ihre Werbung stehen..."
    hi! die Datenbank und die Tabellen haben sich angelegt.
    ​Du mußt das Programm starten und was anlegen. Nachdem du das Programm beendet hast, mußt du den Serverexplorer aktualisieren.

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „hlghyr“ ()

    Hi,
    das Problem scheint zu sein, das ich garnicht verstehe, welche DB ich verbinden soll.

    Der Aufbau ist ja so.
    Projektmappe - Klassenprojekt Bazaar.Core - WinForm Projekt Bazaar.Gui
    Die Datenbank liegt in Bazaar.Core\Database\Bazaar.mdf
    In Bazaar.Gui hab ich noch keine DB Datei drin.

    Wenn ich jetzt über den Paket-Manager-Konsole ein Add-Migration InitialCreate und dann ein Update-Database abschieße, läuft das sauber durch.
    Aber, wenn ich dann im Server Explorer eine Verbindung zur DatenbankDatei Bazaar.Core\Database\BazaarDB.mdf mache, sind dort keine Tabellen angelegt.
    Schaue ich auf LocalDB, gibt es dort eine Datenbank Namens Bazaar.dbo und dort sind Tabellen drin...
    Zur Gui komm ich ja erst garnicht, weil ich auch net genau weis ob ich die Datei in das Projekt rein kopieren muss oder nicht? Oder wie greift die Gui dann auf die Datei in Database\BazaarDB.mdf zu, die ja im Klassenprojekt liegt?
    "Hier könnte Ihre Werbung stehen..."