Wie zu verwenden DbContext ohne Verwendung von Edmx in C#?
Können wir DbContext
ohne Zugabe von EDMX im Projekt für die Daten-Modell-hier ist Beispielcode, in dem ich versuche zu speichern Instance
- Klasse-Objekt mit Hilfe von ContextManager
die DbContext
.
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Objects.DataClasses;
using System.Linq;
using System.Text;
namespace DbContext_test
{
public class ContextManager : DbContext
{
public ContextManager(string connstring)
: base(connstring)
{
}
public override int SaveChanges()
{
//TODO: Write code before saving dataEntity object or fire some event to do so.
return base.SaveChanges();
}
}
public class Instances : EntityObject
{
public int ID { get; set; }
public string InstanceCode { get; set; }
public string InstanceName { get; set; }
}
public class InstanceManager
{
readonly string ConnectionString;
public InstanceManager(string connString)
{
ConnectionString = connString;
}
public void SaveInstance(int id, string instanceCode, string instanceName)
{
SaveInstanceInternal(new Instances { ID = id, InstanceCode = instanceCode, InstanceName = instanceName });
}
public void SaveInstance(Instances instance)
{
SaveInstanceInternal(instance);
}
private void SaveInstanceInternal(Instances instance)
{
var contextManager = new ContextManager(ConnectionString);
contextManager.Entry(instance);
contextManager.SaveChanges();
}
}
}
InformationsquelleAutor Ankush Madankar | 2014-11-06
Du musst angemeldet sein, um einen Kommentar abzugeben.
Kann man es mit den code-first-Ansatz anstelle der edmx-Ansatz.
http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx
InformationsquelleAutor TGH
Folgen Sie diesem
1) erstellen von Kontext-Klasse
2) Klasse erstellen
3) DA Für die Ausführung der Abfrage
}
InformationsquelleAutor