एंटिटी फ्रेमवर्क मॉडल का उपयोग करके डेटा डालें


89

मैं एंटिटी फ्रेमवर्क मॉडल का उपयोग करके अपने डेटाबेस में कुछ डेटा डालने की कोशिश कर रहा हूं, लेकिन मेरे लिए कुछ अज्ञात कारणों से, यह कुछ भी नहीं करता है।

क्या मुझसे कोई चूक हो रही है?

using (var context = new DatabaseEntities())
{
    var t = new test
    {
        ID = Guid.NewGuid(),
        name = "blah",
    };
    context.AddTotest(t);
    context.SaveChanges();
}

'SaveChanges (System.Data.Objects.SaveOptions.AcceptAllChangesAfterSSS) का प्रयास करें;'
विलेम

6
कोड पहले? मॉडल पहले? क्या यह टूटता है या यह सिर्फ कुछ भी स्टोर नहीं करता है? SQL Profiler क्या कहता है? क्या डेटाबेस में कुछ भेजा जा रहा है?
डेनिस ट्रब

जवाबों:


117

यह होना चाहिए:

context.TableName.AddObject(TableEntityInstance);

कहाँ पे:

  1. TableName: डेटाबेस में तालिका का नाम।
  2. TableEntityInstance: तालिका इकाई वर्ग का एक उदाहरण।

यदि आपकी तालिका है Orders, तो:

Order order = new Order();
context.Orders.AddObject(order);

उदाहरण के लिए:

 var id = Guid.NewGuid();

 // insert
 using (var db = new EfContext("name=EfSample"))
 {
    var customers = db.Set<Customer>();
    customers.Add( new Customer { CustomerId = id, Name = "John Doe" } );

    db.SaveChanges();
 }

यहाँ एक जीवंत उदाहरण है:

public void UpdatePlayerScreen(byte[] imageBytes, string installationKey)
{
  var player = (from p in this.ObjectContext.Players where p.InstallationKey == installationKey select p).FirstOrDefault();

  var current = (from d in this.ObjectContext.Screenshots where d.PlayerID == player.ID select d).FirstOrDefault();

  if (current != null)
  {
    current.Screen = imageBytes;
    current.Refreshed = DateTime.Now;

    this.ObjectContext.SaveChanges();
  }
  else
  {
    Screenshot screenshot = new Screenshot();

    screenshot.ID = Guid.NewGuid();
    screenshot.Interval = 1000;
    screenshot.IsTurnedOn = true;
    screenshot.PlayerID = player.ID;
    screenshot.Refreshed = DateTime.Now;
    screenshot.Screen = imageBytes;

    this.ObjectContext.Screenshots.AddObject(screenshot);
    this.ObjectContext.SaveChanges();
  }
}

1
मेरे पास कोई AddObject तरीका नहीं है।
Rocshy

@ डेनिस ट्रब ने आपसे अपने मॉडल के बारे में पूछा। कृपया इसके बारे में अधिक जानकारी प्रदान करें।
दिमित्रीबॉयको

29
का प्रयोग करें। इसके बजाय .AddObject
डेल फ्रेजर

9
EF 6 पर, आप उपयोग करेंगे। इसके बजाय .AddObject
डेविड

1
शानदार जवाब! इसके लिए धन्यवाद क्योंकि यह दिखाता है कि इकाई ढांचे का उपयोग करके कैसे पढ़ना और लिखना है
स्टीफन पीफानिस

41
var context = new DatabaseEntities();

var t = new test //Make sure you have a table called test in DB
{
    ID = Guid.NewGuid(),
    name = "blah",
};

context.test.Add(t);
context.SaveChanges();

करना चाहिए


7

[HttpPost] // जब आप बटन क्लिक करने की घटना पर तर्क लिखते हैं तो इसका उपयोग होता है

public ActionResult DemoInsert(EmployeeModel emp)
{
    Employee emptbl = new Employee();    // make object of table
    emptbl.EmpName = emp.EmpName;
    emptbl.EmpAddress = emp.EmpAddress;  // add if any field you want insert
    dbc.Employees.Add(emptbl);           // pass the table object 
    dbc.SaveChanges();

    return View();
}

यदि कोई प्रश्न मुझसे पूछें
sanket parikh

3

मैं EF6 का उपयोग कर रहा हूं, और मुझे कुछ अजीब लगता है,

मान लें कि ग्राहक के पास पैरामीटर के साथ कंस्ट्रक्टर है,

अगर मैं उपयोग करता हूं new Customer(id, "name"), और करता हूं

 using (var db = new EfContext("name=EfSample"))
 {
    db.Customers.Add( new Customer(id, "name") );
    db.SaveChanges();
 }

यह बिना किसी त्रुटि के चलता है, लेकिन जब मैं डेटाबेस में देखता हूं, तो मुझे वास्तव में पता चलता है कि डेटा सम्मिलित नहीं है,

लेकिन अगर मैं घुंघराले कोष्ठक जोड़ता हूं, तो उपयोग करें new Customer(id, "name"){}और करें

 using (var db = new EfContext("name=EfSample"))
 {
    db.Customers.Add( new Customer(id, "name"){} );
    db.SaveChanges();
 }

डेटा वास्तव में डाला जाएगा,

लगता है घुंघराले ब्रैकेट्स फर्क करते हैं, मुझे लगता है कि केवल जब घुंघराले ब्रैकेट्स जोड़ते हैं, तो इकाई ढांचा यह पहचान लेगा कि यह एक वास्तविक ठोस डेटा है।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.