Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Add Entity Framework to WPF Applications

02.08.2018 (👁9624)

Entity Framework to WPF

 

How to add EF Entity Framework to a WPF application?

 

Under the WPF Project-> References -> Manage NugetPackages

EntityFramework

Entity Framework is Microsoft's recommended data access technology for new applications.

 

 

In the WPF application.

Where you want to use the entity framework you have to enter in the Using area

using System.Data.Entity;       //Entity Framework

 

 

 

Add existing database to WPF

DBFirst

Go to the WPF project in the Solutions Explorer

Then directly or in a subfolder on New Item

 

And under the dialogue: Add New Item to the area Data-> ADO.NET Entity Data Model

A project item for creating Ado.Net Entity Data Model

 

EF Designer from database

Selection on EF Designer from database

Creates a model based on EF Designer based on an existing database. You can choose the database connection, settings for the model, and database objects to include in the model. The classes your application wants to interact with.

 

Then select an existing database. In this example, I put a LocalDB or Service-based Database directly into the project.

This local project database is usually also suggested.

In the lower field under: Save connection settings in App.Config.cs use a suitable general context name like dbContext or database context

Then select the tables and views.

Server views behave in the database like tables without write permissions but read rights.

This will make the database in the application

 

 

The table structure can be found under

Data-> Models.edmx-> Models.tt (then the tables)

You can not write directly to the file because it is created by the designer.

 

The actual tables are located as C # class data models (M) in the subdirectory

 

Access to the database in the application

public static void db_Update_Add_ListRecord(string sURL, string sTitle)

{

    //--------< Add_Record() >--------

    var scan = new tbl_Scans();

    scan.dtCreated = DateTime.Now;

    scan.Title = "First Record";

 

    var db = new dbWebScanEntities();

    db.tbl_Scans.Add(scan);

    db.SaveChanges();

 

    // Display all Blogs from the database 

    var query = from r in db.tbl_Scans

                where r.URLRef==sURL 

                select r;

    //query = query.Take(1);

 

    foreach (var item in query)

    {

        Console.WriteLine(item.Title);

    }

    //--------</ Add_Record() >--------

}

 

 

 

 

Important information for understanding the database:

If you then compile the WPF and test it at runtime, a copy is taken from the design database and placed in the output folder / bin / .. copied.

All data movements are then related to the local runtime database and not to the draft database template.

The design database has only the function of the structure prayer for the local database

 

 

 

 

 

 

 

 

 

 

 

Changes through the database

If changes are made to the database now, they are not automatically transferred to the application

 

Update the data model

To do this, go to Context Designer -> Update Model from Database

 

The system then automatically detects what needs to be done.

You select the changed data and accept it.