سؤال

I have this model on my .mdf DB:

enter image description here

these 2 tables with no data:

enter image description here

PersonID is a foreign key.

This is my EF model diagram:

enter image description here

And this is the code for adding a person.

    namespace DBTest
{
    class Class1
    {
        MyDBEntities db;
        public Class1()
        {
            db = new MyDBEntities();
            AddPerson();
        }        


        void AddPerson()
        {
            Person p = new Person();
            p.ID=1;
            p.NAME="abcd";
            db.AddToPerson(p);
            db.SaveChanges();
        }
    }
}

After I call to the class from the main:

    namespace DBTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 a = new Class1();
        }
    }
}

I want to see if there's any change in my .mdf DB.
I find that the database is still empty (after refreshing):

enter image description here

Does anyone have an idea what I need to do to cause the database to be updated? What am I doing wrong?

EDIT:

The connection string:

    <connectionStrings>
    <add name="DBTest.Properties.Settings.XXXXConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\XXXX.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />
    <add name="MyDBEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDB.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
هل كانت مفيدة؟

المحلول

You didn't show us your connection string - but I'm just guessing from the "symptoms" you report.

The whole User Instance and AttachDbFileName= approach is flawed - at best! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\bin\debug - where you app runs) and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.

The real solution in my opinion would be to

  1. install SQL Server Express (and you've already done that anyway)

  2. install SQL Server Management Studio Express

  3. create your database in SSMS Express, give it a logical name (e.g. MyDB)

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:

    Data Source=.\\SQLEXPRESS;Database=MyDB;Integrated Security=True
    

    and everything else is exactly the same as before...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top