I'm trying to do a very simple rest api for starters. I created the local sqlite file using the tool and it works. Now I want to write a unit test for the database.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace dz1
{
[Table("User")]
public class User
{
[Key]
[Column]
public string Name { get; set; }
[Column]
public string Address { get; set; }
[Column]
public int Age { get; set; }
}
[Table("News")]
public class News
{
[Key]
[Column]
public string Title { get; set; }
[Column]
public string Content { get; set; }
[Column]
[ForeignKey("User")]
public string Author { get; set; }
}
public class MyContext : DbContext
{
private readonly string _connectionString;
public MyContext()
{
this._connectionString = "DataSource=./mydb.db";
}
public MyContext(string connectionString)
{
this._connectionString = connectionString;
}
public DbSet<User> Users { get; set; }
public DbSet<News> News { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite(_connectionString);
}
}
}
and my unit test code:
using System;
using System.Linq;
using dz1;
using Xunit;
namespace ApiTest
{
public class UnitTest1
{
[Fact]
public void Test1()
{
var ctx = new MyContext("DataSource=:memory:");
ctx.Database.EnsureDeleted();
ctx.Database.EnsureCreated();
var u = new User();
u.Name = "Mirko";
u.Address = "Tamo 2";
u.Age = 101;
ctx.Users.Add(u);
ctx.SaveChanges();
Assert.True(ctx.Users.ToList().First().Name == "Mirko");
}
}
}
I read on the internets that EnsureCreated should be enough to create the tables in the in-memory database. What am I missing?
I get an error
Microsoft.EntityFrameworkCore.DbUpdateException : An error occurred while updating the entries. See the inner exception for details.
---- Microsoft.Data.Sqlite.SqliteException : SQLite Error 1: 'no such table: User'.
How do I force the tables to get created?
I even tried using
RelationalDatabaseCreator databaseCreator =
(RelationalDatabaseCreator) ctx.Database.GetService<IDatabaseCreator>();
var s = databaseCreator.GenerateCreateScript();
ctx.Database.ExecuteSqlRaw(s);
but I don't get it. Does the sql script even doesn't run?
s? What does it contain? This will allow you to at least determine whether its the model (ifsis blank and/or missing parts of the schema, then your object model has problems) or the database (ifscontains a valid schema from the model, then something is not working with the in-memory database).EnsureCreated(). Online docs indicate that it returns true or false depending on whether the database was created successfully or not. Your question is valid, but there are so many things that you could be checking... which you are not. Look at docs, trace through the code, etc. Update the question with details you find... or the debugging process just might reveal the answer to you.