Linq2db create table with DateTime2(3) fails when using in memory db and SQLiteDataProvider - sqlite

I am trying to create table in memory db using Linq2Db, and SQLiteDataProvider in a netcore3.1 application.
And if mapping class has a property with attribute
[Column(DataType=DataType.DateTime2, Precision=3), Nullable ]
it gives me the following syntax error :
Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'near ")": syntax error'.
I dig for the query it generates and its this:
CREATE TABLE [testTable]
(
[Id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[Created] DateTime2(3, ) NULL
)
Here is an example that I'm trying:
using System;
using LinqToDB;
using LinqToDB.Data;
using LinqToDB.Mapping;
namespace InMemoryDb
{
class Program
{
static void Main(string[] args)
{
DataConnection.AddConfiguration("default", "Data Source=Sharable;Mode=Memory;Cache=Shared",
new LinqToDB.DataProvider.SQLite.SQLiteDataProvider("SQLite.MS"));
DataConnection.DefaultConfiguration = "default";
using var db = new DataConnection("default");
db.CreateTable<TestTable>();
}
[Table(Schema="dbo", Name="testTable")]
public class TestTable
{
[Column(DataType=DataType.Int32), PrimaryKey, Identity]
public int Id { get; set; }
[Column(DataType=DataType.DateTime2, Precision=3), Nullable]
public DateTime? Created { get; set; }
}
}
}
why its generates query with DateTime2(3, ) and not a correct one DateTime2(3)?

Try this as workaround
[Column(DbType="DateTime2(3)", Nullable ]

You can use possibility of linq2db to define schema for several databases.
Note that there is no DateTime type in SQLite.
[Table(Schema="dbo", Name="testTable")]
public class TestTable
{
[Column(DataType=DataType.Int32), PrimaryKey, Identity]
public int Id { get; set; }
[Column(Configuration=ProviderName.SQLite, DataType=DataType.DateTime2), Nullable]
[Column(DataType=DataType.DateTime2, Precision=3), Nullable]
public DateTime? Created { get; set; }
}

Related

SQLite.NET PCL returning 0 in all instances of autoincrement primary key

I am totally not getting this, because I have used this library in Xamarin apps for several years.
I have this base class that contains properties common in all db items:
public class BaseItem
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; } = 0; // SQLite ID
public long CreatedTimeSeconds { get; set; } = DateTime.Now.ToUnixTimeSeconds();
public long ModifiedTimeSeconds { get; set; } = DateTime.Now.ToUnixTimeSeconds();
}
Now, I derive from it:
[Table("CategoryTable")]
public class Category : BaseItem
{
public int CategoryTypeID { get; set; } = (int)CategoryType.Invalid;
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
}
Here's a simplified version of what I'm seeing:
public class DBWorld
{
ISQLiteService SQLite { get { return DependencyService.Get<ISQLiteService>(); } }
private readonly SQLiteConnection _conn;
public DBWorld()
{
_conn = SQLite.GetConnection("myapp.sqlite");
}
public void TestThis()
{
_conn.CreateTable<Category>();
var category = new Category();
category.Name = "This Should Work";
int recCount = connection.Insert(category);
// at this point recCount shows as 1, and category.ID shows as zero.
// I thought Insert was supposed to set the autoincrement primary key
// regardless, it should be set in the database, right? So...
var categoryList = connection.Query<Category>($"SELECT * FROM {DBConstants.CategoryTableName}");
// at this point categoryList[0] contains all the expected values, except ID = 0
}
}
I am obviously missing something, but for the life of me, I can't figure out what...
Like so many other bizarre things that happen in the Visual Studio Xamarin world, when I went back later, this worked the way all of us expect. I guess Visual Studio was just tired and needed to be restarted.

Entity Framework modify models

I'm trying to modify the model entity which is sent to the CreateEmployee Method as a parameter to be modified.
public void CreateEmployee(string roleName, EmployeeModel emp)
{
string roleName == "user";
emp.Roles.Select(e => new RoleModel { RoleName = roleName });
AddEmployee(emp);
}
this is how models looks like....
And it give me an error saying
Model does not contain a definition for 'Select' and 'Select' accepting a first argument of type Model could be found....
I have tried using the 'Where' method as well, but still give the same error..
emp.Roles.Where(e => e.RoleName == roleName)
You have some errors in your code,
use = not == for assignment
the selected roles does not used
Linq query functions like Select and Where apply to IEnumerable the Roles property is of type RoleModel not IEnumerable<RoleModel>
so:
public class EmployeeModel
{
//some code
public ICollection<RoleModel> Roles{ get; set; }
}
using System.Linq;
public void CreateEmployee(string roleName, EmployeeModel emp)
{
string roleName = "user";
var empRoles = emp.Roles.Select(e => new RoleModel { RoleName = roleName });
AddEmployee(emp);
}
Edit:
If you want to have one role per employee at most, the relationship between Role and Employee became one-to-many(each role has n employee)
if Role is optional for employee:
public class EmployeeModel
{
//some code
public short? RoleId { get; set; }//nullable foreign key
public virtual RoleModel { get; set; } //Navigation property
}
public class RoleModel
{
//some code
public ICollection<Employee> Employees{ get; set; }
}
But, if Role is required for Employee you must change the foreign key to:
public short RoleId { get; set; }//non-null foreign key
For getting an Employee's Role you do not need to use Select or Where on navigation property, just use employee.Role.
Finally for querying Role's employees you can use Select, Where, ... as mentioned before.

Cannot insert the value NULL into column error

I am attempting to save user preferences into a table but am getting a null exception and I do not understand why. This is an MVC 4 application and this is my action result where I am getting the error.
public ActionResult Go(string path, string name)
{
RaterContext r = new RaterContext();
UserData u = new UserData();
var userid = u.GetCurrentUserData().UserId;
var info = r.RatersInfo.Where(w => w.RaterName.Equals(name)).FirstOrDefault();
var pref = r.RatersPreferences.Where(w => w.RaterId.Equals(info.RaterId) && w.UserId.Equals(userid)).FirstOrDefault();
if (pref != null && pref.Count > 0)
{
pref.Count++;
r.SaveChanges();
}
else
{
pref = new RaterPreferences();
pref.UserId = userid;
pref.RaterId = info.RaterId;
pref.Count = 1;
r.RatersPreferences.Add(pref);
r.SaveChanges();
}
return Redirect(path);
}
There is nothing saved in the preferences table yet so it is hitting the else block and throwing a null exception on r.SaveChanges();. The exception is
Cannot insert the value NULL into column 'UserId', table
'WebSiteNew.dbo.RaterPreferences'; column does not allow nulls. INSERT
fails.\r\nThe statement has been terminated.
The reason this doesn't make sense is because all three properties, including the UserId have data when I step through. These are the only fields in the table. UserId = 1, RaterId = 6 and Count is clearly set to 1. They are all set as non-nullable ints and the primary key is a combination of UserId and RaterId. My Model is as follows.
public class RaterContext : DbContext
{
public RaterContext()
: base("DefaultConnection")
{
}
public DbSet<RaterInfo> RatersInfo { get; set; }
public DbSet<RaterPreferences> RatersPreferences { get; set; }
}
[Table("RaterInfo")]
public class RaterInfo
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int RaterId { get; set; }
public string RaterName { get; set; }
public string RaterLink { get; set; }
public string Section { get; set; }
public string Department { get; set; }
}
[Table("RaterPreferences")]
public class RaterPreferences
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public int RaterId { get; set; }
public int Count { get; set; }
}
Any help would be greatly appreciated as I am relatively new to MVC and ASP.NET. Let me know if you need more information. Thanks in advance!
I don't know if this helps but I tested to see what would happen on UPDATE by adding data manually so it would catch on the if block and that works. I'm only getting an error on INSERT.
Here is the create statement for the table in question.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[RaterPreferences](
[UserId] [int] NOT NULL,
[RaterId] [int] NOT NULL,
[Count] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[UserId] ASC,
[RaterId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[RaterPreferences] WITH CHECK ADD CONSTRAINT [FK_RaterPreferences_RaterInfo] FOREIGN KEY([RaterId])
REFERENCES [dbo].[RaterInfo] ([RaterId])
GO
ALTER TABLE [dbo].[RaterPreferences] CHECK CONSTRAINT [FK_RaterPreferences_RaterInfo]
GO
I have copied your code into a brand new ASP.Net MVC project with the current version of Entity Framework and I am able to run your code with no problems. I escaped the UserData acquisition with code that looks like:
RaterContext r = new RaterContext();
//UserData u = new UserData();
var userid = 1; // u.GetCurrentUserData().UserId;
var info = r.RatersInfo.Where(w => w.RaterName.Equals(name)).FirstOrDefault();
and did not have a problem running the remainder of this code.
I think you may have some problems with your keys and database structure for the RaterPreferences table. I don't know your full data-model, but I don't understand how this fits in, and it is not keyed in your code the way that you describe.
Edit:
I've modified my database tables to reflect the design you've described. You have a difference between your EntityFramework code-first implementation and your database. It looks like your database existed first, and I would remove your EntityFramework classes and rebuild them with Database First techniques.

How to create database and table in sqlite programatically using monotouch?

Can any body help me about, how to create simple database and tables programatically and insert values ,step by step procedure in sqlite using monotouch.I am new to this technology .
Thank you..
If you're using sqlite-net (which I highly recommend) you can simply call:
Model
public class Stock
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(8)]
public string Symbol { get; set; }
}
Create
var db = new SQLiteConnection("stocks.db");
db.CreateTable<Stock>();
Insert
var s = db.Insert(new Stock() {
Symbol = symbol });
Query
return db.Query ("select * from Valuation where StockId = ?", stock.Id);

FluentNHibernate error -- "Invalid object name"

I'm attempting to do the most simple of mappings with FluentNHibernate & Sql2005. Basically, I have a database table called "sv_Categories". I'd like to add a category, setting the ID automatically, and adding the userid and title supplied.
Database table layout:
CategoryID -- int -- not-null, primary key, auto-incrementing
UserID -- uniqueidentifier -- not null
Title -- varchar(50) -- not null
Simple.
My SessionFactory code (which works, as far as I can tell):
_SessionFactory = Fluently.Configure().Database(
MsSqlConfiguration.MsSql2005
.ConnectionString(c => c.FromConnectionStringWithKey("SVTest")))
.Mappings(x => x.FluentMappings.AddFromAssemblyOf<CategoryMap>())
.BuildSessionFactory();
My ClassMap code:
public class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
Id(x => x.ID).Column("CategoryID").Unique();
Map(x => x.Title).Column("Title").Not.Nullable();
Map(x => x.UserID).Column("UserID").Not.Nullable();
}
}
My Class code:
public class Category
{
public virtual int ID { get; private set; }
public virtual string Title { get; set; }
public virtual Guid UserID { get; set; }
public Category()
{
// do nothing
}
}
And the page where I save the object:
public void Add(Category catToAdd)
{
using (ISession session = SessionProvider.GetSession())
{
using (ITransaction Transaction = session.BeginTransaction())
{
session.Save(catToAdd);
Transaction.Commit();
}
}
}
I receive the error
Invalid object name 'Category'.
Description: An unhandled exception
occurred during the execution of the
current web request. Please review the
stack trace for more information about
the error and where it originated in
the code.
Exception Details:
System.Data.SqlClient.SqlException:
Invalid object name 'Category'.
I think it might be that I haven't told the CategoryMap class to use the "sv_Categories" table, but I'm not sure how to do that.
Any help would be appreciated. Thanks!
Use the Table method in your ClassMap.
public class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
Table("sv_Categories");
}
}

Resources