I
I
Ivan Murashkin2017-09-09 18:53:57
C++ / C#
Ivan Murashkin, 2017-09-09 18:53:57

Why does code execution stop at the line of adding to the database (C#, EF6)?

Program.cs code

using System;

namespace FirstEF6App {
    class Program {
        static void Main(string[] args) {
            using (UserContext db = new UserContext()) {

                User user1 = new User { Name = "Tom", Age = 33 };
                User user2 = new User { Name = "Sam", Age = 26 };
                //Прошелся пошаговым выполнение, на 15 строке вызываеться консоль и все...Дальше выполнение не идет
                db.Users.Add(user1);
     //           db.Users.Add(user2);
                db.SaveChanges();
                Console.WriteLine("Обьекты успешно сохранены");

                var users = db.Users;
                Console.WriteLine("Список обьектов");
                foreach (User u in users) {
                    Console.WriteLine("{0}.{1} - {2}", u.Id, u.Name, u.Age);
                }
            }
            Console.ReadLine();
        }
    }
}

User.cs code
namespace FirstEF6App {
    public class User {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

Code UserContext.cs
using System.Data.Entity;

namespace FirstEF6App {
    class UserContext : DbContext {

        public UserContext()
            : base("DbConnection") { }

        public DbSet<User> Users { get; set; }

    }
}

App.config code
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="DbConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Userstore;Integrated Security=True;"
         providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Software versions: VS 15.3; SQLEXPRESS 2014Adv; EF6

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Murashkin, 2017-09-10
@IvanRUSH

I found a solution, it turns out you can’t name the table “User”, I don’t understand why, maybe after the update (maybe I just didn’t know before :)) they reserved this word and something else, but I have it. with the CodeFirst approach. just a table with that name should be generated and now

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question