System.MissingMethodException when creating table in sqlite-net-pcl - c#

When calling for this function to create the database, an error occurs System.MissingMethodException: 'Method not found: int SQLite.SQLiteConnection.CreateTable(SQLite.CreateFlags)'
This is where it throws the error during the call for database = new TransmodLocalDB();
App.xaml.cs
public static TransmodLocalDB Database
{
get
{
if (database == null)
{
database = new TransmodLocalDB(0);
}
return database;
}
}
And here is the creation of the tables and the call could not proceed to here.
TransmodLocalDB.cs
public TransmodLocalDB()
{
try
{
database = DependencyService.Get<ISQLite>().GetConnection();
// create the tables
database.CreateTable<Account>(CreateFlags.None);
database.CreateTable<Driver>(CreateFlags.None);
database.CreateTable<Job>(CreateFlags.None);
database.CreateTable<TransportLeg>(CreateFlags.None);
database.CreateTable<Notes>(CreateFlags.None);
database.CreateTable<ImageNote>(CreateFlags.None);
database.CreateTable<Sync>(CreateFlags.None);
database.CreateTable<Enums>(CreateFlags.None);
} catch (Exception ex)
{
var me = ex.Message;
}
}
The exceptions throws before it can create the tables.
Used this library sqlite-net-pcl by Frank A. Krueger latest version 1.6.292

Related

Get last record using C# MongoDB driver

I am using the mongo driver for a API. The name of my db is "EventsDb". I need to insert an entity that I have and that is "Event". I need to return the last register of the mongo db:
T IRepository<T>.Add(T entity)
{
try
{
this.mongoCollection.InsertOne(entity);
var response = this.mongoCollection.Find<Event>("EventsDb").limit(1).sort();
return response;
}
catch (Exception e)
{
throw new NotImplementedException(e.ToString());
}
}
This is the problem I am having:
Severity Code Description Project File Line Suppression State
Error CS1929 'IMongoCollection' does not contain a definition for
'Find' and the best extension method overload
'IMongoCollectionExtensions.Find(IMongoCollection,
FilterDefinition, FindOptions)' requires a receiver of type
'IMongoCollection' Events.Repositories
How do I fix this error?
This is my solution for this problem I added a librarie
using MongoDB.Driver.Linq;
And after
public T Add(T entity)
{
try
{
this.mongoCollection.InsertOne(entity);
var response = this.mongoCollection.AsQueryable().OrderByDescending(c => c.Id).First();
return response;
}
catch (Exception e)
{
throw new NotImplementedException(e.ToString());
}
}

EF returns ExecuteReader requires an open and available Connection. The connection's current state is open. error

I have a lot of classes with this structure as you can see:
public class OrganizationUserRepository : IOrganizationUserRepository
{
private DataContext _ctx;
public OrganizationUserRepository(DataContext ctx)
{
_ctx = ctx;
}
public bool Add(OrganizationUser entity)
{
try
{
_ctx.OrganizationUsers.Add(entity);
_ctx.SaveChanges();
return true;
}
catch (Exception ex)
{
// TODO log this error
return false;
}
}
public bool Edit(OrganizationUser entity)
{
try
{
OrganizationUser Edited = _ctx.OrganizationUsers.Where(i => i.Id == entity.Id).First();
_ctx.Entry(Edited).CurrentValues.SetValues(entity);
_ctx.SaveChanges();
return true;
}
catch (Exception ex)
{
// TODO log this error
return false;
}
}
public bool Remove(string id)
{
try
{
Int64 Id = Int64.Parse(id);
OrganizationUser obj = _ctx.OrganizationUsers.Where(i => i.Id == Id).First();
_ctx.OrganizationUsers.Remove(obj);
_ctx.SaveChanges();
return true;
}
catch (Exception ex)
{
// TODO log this error
return false;
}
}
}
The db context in constructor is injected by ninject .as you can see it just one of my classes .and i have multi classes like this in another services that use a single DB .(WCF Service).But i get this error in my wcf tracelog :
ExecuteReader requires an open and available Connection. The connection's current state is open.
I am using EF code first .
I found this Wrap DbContext db = new DbContext() inusing statement. And i want to know should i use this ,if Yes how can i change my class structure to use using in my code ?
I used public Readonly DbContext .i just remove readonly and everything work fine.

How to store long text (about 4000000 character) in SQL Server 2014 by Entity Framework 6

When I try to save my Article object that contains a field of string type (about 4000000 character) in a database table that maps to a nvarchar(max) column, Entity Framework could not send the value of that string from C# application to the database.
How can I solve this problem?
Here is my model
public class Article
{
public int Id { get; set; }
public string Authors { get; set; }
}
here is the saving part
private void EditDatabase(Article article)
{
try
{
var db = new ArticleContext();
db.Entry(article).State = EntityState.Modified;
db.SaveChanges();
db.Dispose();
}
catch (Exception ex) { }
}
Did you just create that Article object before? Or did you retrieve it from the database?
Anyway - you're just changing it's state - but you're not attaching it to the context - therefore it won't be saved:
private void EditDatabase(Article article)
{
try
{
using (var db = new ArticleContext())
{
// assuming you're *created* that "article" from scratch
// then you need to **ADD** it to the context first, to save it!
db.Articles.Add(article);
// this won't be needed
// db.Entry(article).State = EntityState.Modified;
db.SaveChanges();
}
}
catch(Exception exc)
{
// Log your exception
}
}
Update:
If that article has already been stored in the database and handled by another context before being passed into EditDatabase, then you must attach that pre-existing database object to the new context you create and set its State to Modified:
private void EditDatabase(Article article)
{
try
{
using (var db = new ArticleContext())
{
// assuming this "article" has been loaded from the database
// by another context before, then you need to **ATTACH** it
// to this current context before you can save it here!
db.Articles.Attach(article);
db.Entry(article).State = EntityState.Modified;
db.SaveChanges();
}
}
catch(Exception exc)
{
// Log your exception
}
}

Intermittent issue with Entity Framework

I am using Entity Framework 6.1 version.
I am performing a bulk insert operation with Entity Framework. I am getting an intermittent error with DbContext:
object reference not set to an instance of object" for db context.
Here is my code:
public class ApiTimingDataProvider : IApiTimingDataProvider
{
private ApiTimingDbContext _apiDbContext;
public ApiTimingDataProvider(ApiTimingDbContext apiDbContext)
{
if (apiDbContext == null)
throw new ArgumentNullException("ApiDbContext is Null");
_apiDbContext = apiDbContext;
}
public void InsertApiData(List<ApiTimingModel> apiModelList)
{
try
{
using (var ctx = _apiDbContext)
{
using (var transactionScope = new TransactionScope())
{
// some stuff in dbcontext
ctx.Database.CommandTimeout = 600;
ctx.BulkInsert(apiModelList);
ctx.SaveChanges();
transactionScope.Complete();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}
Can anyone suggest me the reason for getting the exception?

how do I update entity object passed from another class?

This is my query where I'm returning an IEnumerable<CreditCardTransaction> to iterate through.
public partial class CreditCardTransaction
{
public static IEnumerable<CreditCardTransaction> GetUnprocessedTransactions()
{
try
{
using (var context = new SuburbanEntities())
{
return from trans in context.CreditCardTransactions
where trans.IsPublished == false
select trans;
}
}
catch (Exception ex)
{
Logging.Log("An error occurred.", "GetUnprocessedTransactions",Apps.ServicesConfig, ex);
return null;
}
}
}
This is where I am modifying those transactions once I have processed them:
public void ProcessFile()
{
try
{
_client = new TruckServiceClient();
_globalSetting = new GlobalSetting();
var unprocesstransactions = CreditCardTransaction.GetUnprocessedTransactions();
foreach (var creditCardTransaction in unprocesstransactions)
{
creditCardTransaction.IsPublished = ProcessTransaction(creditCardTransaction);
}
}
catch (Exception ex)
{
Logging.Log("An error occurred.", "ProcessCreditCardTransactions.ProcessFile", Apps.RemoteServices, ex);
}
}
I am modifying the transactions here:
creditCardTransaction.IsPublished = ProcessTransaction(creditCardTransaction);
But once I have saved them, can I update the entity directly or do I need to create another method where I pass this information back in?
The problem you don't have access to the context. Here you have some examples how to do:
https://github.com/geersch/EntityFrameworkObjectContext
If you're developing Asp.Net app, you'll have some drawbacks illustreted in this article:
http://dotnetslackers.com/articles/ado_net/Managing-Entity-Framework-ObjectContext-lifespan-and-scope-in-n-layered-ASP-NET-applications.aspx#managing-objectcontext-instantiation

Categories

Resources