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
For some reason, i am unable to generate database table using code first approach. Framework:
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.