1

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();
        }    
    }
}
5
  • What exception does it throw? When you have an error, always show the error message and stack trace - it has an answer to a question in 99% times. Commented Aug 8, 2016 at 6:14
  • Sorry i forgot it 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. Commented Aug 8, 2016 at 6:15
  • 1
    There is also an InnerException, post its contents as well. Commented Aug 8, 2016 at 6:21
  • what is the inner exception?? Commented Aug 8, 2016 at 7:33
  • .NET Exception object has a property InnerException of type Exception. So, if your exception is generated because of another exception, or depends on it, you can store it there to describe the problem better. Actually, there can be a long InnerExceptions chain. In case of EntityFramework, most errors are located in InnerException object. Just use this property to access it: catch (Exception e) { Console.WriteLine(e.InnerException); } Commented Aug 8, 2016 at 7:38

1 Answer 1

1

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:)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.