Entity Framework Core 2.2.3 not creating table using code-first approach vs 2017

c# entity-framework-core

Question

HI I am trying to learn EntityFramework Core from following tutorials http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx

So far i have created

  • Domain Class
  • DbContextImpl Class
  • Execution class

For some reason, i am unable to generate database table using code first approach. Framework:

  • .Net Core 2.2
  • EF Core 2.2.3[Edited]
  • SQL Server v17.9.1

My code is as follows:

using Microsoft.EntityFrameworkCore;

namespace EntityFrameworkTuts.repo
{
    public class TwitterDbContext: DbContext
    {
        public DbSet<User> Users { get; set; }

        public DbSet<Tweet> Tweet { get; set; }

        public TwitterDbContext() : base()
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Data Source=sys-win;Initial Catalog=milton;Persist Security Info=True;User ID=sa;Password=password");
        }


    }
}

namespace EntityFrameworkTuts
{
    public class User
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public ICollection<Tweet> Tweets { get; set; }
    }
}

namespace EntityFrameworkTuts
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            using (var context = new TwitterDbContext())
            {
                User user = new User
                {
                    Name = "John Doe",
                };

                context.Users.Add(user);
                var result = context.SaveChanges();
                Console.WriteLine($"save user ${user.Name} with id ${result}");
            }
        }
    }
}

I am getting exception below:

Unhandled Exception: Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid object name 'Users'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)

I done small research and it looks like framework is not able to find Users tables.

Should EFCore 2.2.3 automatically create one? Please let me know what silly mistake i might be doing.

EDITED: Thanks Marc for clearing versioning doubt.

1
1
3/12/2019 7:00:22 AM

Popular Answer

Thanks to @er-sho, i managed to generate database using code first approach. Following things helped me:

Go to Tools => Nuget Package Manager => Package Manager Console.

For creating db migration code

dotnet ef migrations add CreateDatabase --output-dir Data/Migrations

Execute migration code:

dotnet ef database update

0
3/12/2019 7:20:21 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow