Berkin
Berkin

Reputation: 1664

Entity Framework, Adding Data To Database

We have a problem while we are trying all of the data to database. We are using Entity Framework Code First method and SQL Server. Our connections are ready, our tables have been created.

We are keeping our data into List right now. But we cant send it to database. Its our code. When code comes to Save.Changes line, it crushes

ITS THE ERROR CODE

An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code

Additional information: An error occurred while updating the entries. See the inner exception for details.

CONTACT ENTITY

public class Contact 
{
    [Key]
    public int Id { get; set; }
    public string cn { get; set; }
    public string sn { get; set; }
    public string c { get; set; }
    public string l { get; set; }
    public string st{ get; set; }
    public string title{ get; set; }
    public string postalCode { get; set; }
    public string physicalDeliveryOfficeName{ get; set; }
    public long? telephoneNumber{ get; set; }
    public string givenName{ get; set; }
    public string initials { get; set; }
    public DateTime? whenCreated { get; set; }
    public DateTime? whenChanged { get; set; }
    public string co{ get; set; }
    public string displayName{ get; set; }
    public int? delivContLength { get; set; }
    public string company{ get; set; }
    public string proxyAdress{ get; set; }
    public string streetAdress{ get; set; }
    public string mailNickname{ get; set; }
    public string name{ get; set; }
    public int? primaryGroupID { get; set; }
    public string objectGUID { get; set; }
    public string objectSID{ get; set; }
    public string sAMAccountName{ get; set; }
    public string mail{ get; set; }
    public string homePhone { get; set; }
    public string mobile { get; set; }
}

ENTITY FRAMEWORK PAGE

namespace WebApplication5.EntityFramework
{
    public class PhoneDexContext : DbContext

    { 
        public DbSet<Contact> Contacts { get; set; }
        public DbSet<SyncInfo> SyncInfo { get; set; }
    }
}

DATABASE SECTION

namespace WebApplication5.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            var test = new LdapServiceManager().getAllUsers();
            var phoneDex = new PhoneDexContext();

            foreach (var contact in test)
            {

                //phoneDex.Entry(contact).State = System.Data.Entity.EntityState.Added;
                phoneDex.Contacts.Add(contact);

                //TODO HATA ALINIYOR
                phoneDex.SaveChanges();

            }

            return View();
        }    
    }
}

Upvotes: 1

Views: 193

Answers (1)

Berkin
Berkin

Reputation: 1664

It's done guys, thanks.

It's about another database that we didn't create. We are using localhost database's connection string but there was another one which created by template. After deleting template's database, its solved:)

Upvotes: 1

Related Questions