C# adding table object to entity and saving changes not working - c#

I'm learning about ADO.Net entity framework, and
I'm stuck at adding entity table object to the database.
I have a local database in solution called testDB.
It has two columns - id(primary, unique, identiy),name(varchar(100))
and entity for it. The main application code is here below.
Problem is, that using this code it doesn't add anything to the table, but also
I'm not having any errors.
What could go wrong?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace entityproject
{
class Program
{
static void Main(string[] args)
{
String someString;
someString = "Just a test";
testDBEntities tdbEntity = new testDBEntities();
test tblTest = new test();
tblTest.name = someString;
tdbEntity.test.Add(tblTest);
tdbEntity.SaveChanges();
}
}
}
App.config here
http://pastie.org/6980938

I think I know the problem - had the same thing last time I tried using SQL CE.
The connection string doesn't point to the sdf file that you created - it uses a new one that I believe gets put into your bin\Debug or bin\Release directory (in your config file as data source=|DataDirectory|\testDB.sdf), with your EXE and DLLs. If you check that directory, I bet you'll find another sdf file there, that has a bunch of records added.
If you want to use the sdf that you've already created, change the connection string to point specifically to that file.

Related

'file is not a database' or 'No migrations were applied. The database is already up to date.' when creating Database.sqlite in non-default path

I am an extremely novice C# programmer following a series of video tutorials using Discord.net and sqlite to create a bot for my server to add various commands and automate various tasks.
Everything has gone fine in the tutorial until I reached the sixth installment, https://www.youtube.com/watch?v=-Lf6w7WZvT0. A little after 12:30, the author attempts to use update-database in the Package Manager Console to create a database named Database.sqlite at a non-default location. However, attempting to create this database the way he has it written results in an error, SQLite Error 26: 'file is not a database'.
The tutorial author's solution is to create the database at the default location, manually move it to the preferred location, then edit the code to use the preferred location. To me this seems inelegant and I'd like to figure out how to make the Package Manager Console create the progam in the correct location.
Here is the program as the tutorial author originally writes it
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace MyBot.Resources.Database
{
public class SqliteDbContext : DbContext
{
public DbSet<Stone> Stones { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder Options)
{
string DbLocation = string DbLocation = Assembly.GetEntryAssembly().Location.Replace(#"bin\Debug\netcoreapp2.1",
#"Data\Database.sqlite");
Options.UseSqlite("Data Source=" + DbLocation);
}
}
}
This results in the file is not a database error.
He then changes this to
string DbLocation = string DbLocation = Assembly.GetEntryAssembly().Location.Replace(#"bin\Debug\netcoreapp2.1",
#"Data\Database.sqlite");
Options.UseSqlite("Data Source=Database.sqlite"_;
This creates a Database.sqlite file in the MyBot directory, which the author then moves to the MyBot\Data directory. He then changes the program to
string DbLocation = string DbLocation = Assembly.GetEntryAssembly().Location.Replace(#"bin\Debug\netcoreapp2.1",
#"Data\");
Options.UseSqlite($"Data Source={DbLocation}Database.sqlite";
And from there the program runs as expected.
I deleted the Database.sqlite file and reran the code as it is above to see what happens, and got the message No migrations were applied. The database is already up to date.
Rerunning the various steps leads to the same result each time.
I've attempted to google for the two error messages and haven't found anything useful. I also checked the api page for DbContextOptionsBuilder at https://learn.microsoft.com/en-us/ef/core/api/microsoft.entityframeworkcore.dbcontextoptionsbuilder and couldn't find anything that would indicate why it wasn't working.
Any help getting the Database.sqlite file to write to MyBot\Data would be much appreciated.

Entity Framework isn't saving data to database in WPF

I'm developing a POS in WPF.
For CRUD operations, I'm using Entity Framework.
Created a WPF View ProductADD
Product Add View Snap
Created a Class ProductController in Controller Folder
Made object of Entity Framework in ProductController Class ProductController Calss Snap
Created a method: SaveProduct(Product product) which is taking product object as argument and saving it to database using EF.
And From Xaml.Cs I'm calling ProductController Class's Saveproduct method and sending the new product data to it.
private void Button_Click(object sender, RoutedEventArgs e)
{
ProductController pc = new ProductController();
PRODUCT product = new PRODUCT();
product.PRODUCT_NAME = Product_Name.Text.ToString();
product.UNITPRICE = Convert.ToInt32(Unit_Price.Text.ToString());
product.CATEGORY_Id = 1;
pc.SaveProduct(product);
MessageBox.Show("Product Added Successfully");
this.Close();
}
And in ProductController the following code is updating the database
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PizzaLounge.Models;
namespace PizzaLounge.Controllers
{
public class ProductController
{
PizzaLoungeEntities db = new PizzaLoungeEntities();
public void SaveProduct(PRODUCT product)
{
db.PRODUCTs.Add(product);
db.SaveChanges();
}
}
}
The code executes successfully but it doesn't save the product in database. P.S. I have used db.savechanges().
Am I missing something or using wrong approach to update database?
You are probably using |DataDirectory| in your connection string. If debugging in Visual Studio, the database you are using is in the bin/debug folder.
Unfortunately if you look at the db through Server Explorer it has a different connection string so you don't see the changes.
Also if the database property "Copy to Output directory" is set to Copy Always then every time you debug you will overwrite your db and you won't see the data you added. You can check if this is happening by using a new db context in the same debug session where you add the records. If the new context can get the records from the db then you know they must be being written (as well as the other checks listed in the comments)
This can be fixed by changing Copy To Output Directory to Never Copy or Copy If Newer.
Because you are using mdf file attached to your project so your problem is like this question
Attaching database to my project
You are saving data to database that is in bin\debug folders ,and then you see the mdf file that is in your project folder and you don't see the data .
change your connection string from DataDirectory to
absolute path to the project database file. When deploying, just change it back to |DataDirectory|
How did you checked that the products haven't been added, from db.PRODUCTs or from the Database Explorer?
Maybe you just need to dispose your context, change your ProductController for something like:
using(var db = new PizzaLoungeEntities()){
db.PRODUCTs.Add(product);
db.SaveChanges();
}
Or to dispose ProductController after you finish to use it.

Kentico UserInfoProvider not working as expected in a console app

This code works fine within a Kentico website:
var users = UserInfoProvider.GetUsers();
for (int x = 0; x < users.Count(); x++
{
UserInfo currentUser = users.ElementAt(x);
currentUser.SetValue("AcceptsAlerts", equivalentSubscriber.Status != SubscriberStatus.Unsubscribed);
UserInfoProvider.SetUserInfo(currentUser);
}
When I move the code to a console app, any calls to UserInfoProvider result in the error: "Object type 'cms.usersettings' not found"
For the initial call to get the users, I can do it like this in the console app:
DataSet usersds = new CMS.DataEngine.DataQuery("cms.user.selectall").Execute();
then loop through Table1 of the dataset using the user data:
UserInfo currentUser = new UserInfo(dtUsers.Rows[x]);
All is fine and working, until I come to write the updated user back to the database. I cannot find another way of writing the data apart from calling:
UserInfoProvider.SetUserInfo(currentUser);
Does anyone know another way to save the user data? or to resolve the error. The error is a runtime error and as far as I know, I have referenced everything I need to. The field I am editing is a custom field added to the cmsUser table.
using statements for info:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using CMS;
using CMS.CustomTables;
using CMS.DataEngine;
using CMS.Membership;
Before you start working with Kentico CMS API from an external application make sure you call the following lines:
CMS.DataEngine.ConnectionHelper.ConnectionString = "your connection string";
CMS.Base.SystemContext.WebApplicationPhysicalPath = Application.StartupPath;
CMS.DataEngine.CMSApplication.Init();
Then, you'll be also able to use UserInfoProvider.GetUsers() object query instead of using DataQuery.Execute().
Are you sure you are referencing all necessary assemblies?
Following scenario works on my machine with configuration: Kentico 8.x, Web Application project
Reference in your Console application these assemblies from lib folder
CMS.Base
CMS.DataEngine
CMS.DataProviderSQL
CMS.Membership
Then copy your Connection String from Web Application's web.config to Console Application's App.config.
After that you can use this code to set custom user properties
static void Main(string[] args)
{
var users = UserInfoProvider.GetUsers();
foreach (var user in users)
{
user.SetValue("myTestString", "test");
user.Generalized.SetObject();
}
}
For anyone looking to get a SiteID to use in API calls from an external app such as getting an email template, this might help you. In Kentico 8.1 you can go to Sites > General and get the code name for your site. Then you can do this:
int siteID = CMS.SiteProvider.SiteInfoProvider.GetSiteID("<your site code name>");
Hope it helps!

accessing a dataset and table adapter from a new class

I created a working Windows Forms project in C# that accesses an Access database of recipes. Using table adapters and the dataset I am able to query, update and insert new data into the database.
I created a new VS project, imported my database and started a new class. My problem is when I try to set up the table adapters I get errors, specifically with the .Fill() method. When I check the datasource.xsd I can see that the .get() and .Fill() methods were created but I can't seem to access them like I did when I just dragged and dropped the binding source onto the WinForm previously.
I copied the code to programmatically create the table adapters from the MSDN website but I get the error on the line where I call ingredientTableAdapter.Fill(recipiesNewDataSet); method. Any one have a clue why? Here's my code on this project so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyRecipeDataBase
{
class QueryClass
{
recipiesNewDataSet recipiesNewDataSet = new recipiesNewDataSet();
recipiesNewDataSetTableAdapters.IngredientTableAdapter ingredientTableAdapter = new recipiesNewDataSetTableAdapters.IngredientTableAdapter();
ingredientTableAdapter.Fill(recipiesNewDataSet);
}
}
The answer was staring me in the face!
You're putting code directly in the class body. It should be inside a function. Inside the class constructor, for instance.
class QueryClass
{
public QueryClass()
{
recipiesNewDataSet recipiesNewDataSet = new recipiesNewDataSet();
recipiesNewDataSetTableAdapters.IngredientTableAdapter ingredientTableAdapter = new recipiesNewDataSetTableAdapters.IngredientTableAdapter();
ingredientTableAdapter.Fill(recipiesNewDataSet);
}
}

C# reference to a dll file i created doesn't work as it should

i'm using microsoft visual C# 2010 express to write a form program to read and write to an access database.
i created a class that is designed to read/write to the database file, saved it under a namespace and created a dll from it.
it is set as ".net Framework 4"
in my main program i added the reference to the dll file but when i try to add it to the code with
using Database;
it won't work even that the Database is in the reference of the namespace.
am i doing something wrong? or is there another way to use the commands from Database in my main program other then copying it to it?
// update //
solved
added public to all database public and DataBase db = new DataBase();
DATABASE.cs is use it for dll
using System;
using System.Collections.Generic;
using System.Data.OleDb;
namespace Database
{
public class DataBase
{
public DataBase()
{
}
public void ItemInsert(string name,string creator,string publishing,string itemType,string genere, string year)
the main program
using System;
using System.Windows.Forms;
using Database;
namespace library
{
public partial class newItemForm : Form
{
private void btnConfirmNewItemClick(object sender, EventArgs e)
{
DataBase db = new DataBase(); //this solved it
db.ItemInsert(txtItemNameType.Text, txtEditorType.Text, txtCreatorType.Text, comboBoxType.Text, txtGenereType.Text, txtYearType.Text);
}
}
}
You also need to Add a Reference to said assembly in your current project. The using statement brings a referenced assembly into scope...
right click you project in visual studio, select add refrence then choose Browse tab, then find the poject folder and get in bin -> debug and then you will see the dll choose it. visual studio will add it to your refrences, now you need to add a using on top of the pages you want it like this:
using mydllName;
if you didnt find your dll:
Load the librery project agian and right click in visual studio and press Build it will generate the dll.
You must add a reference to the assembly you created. The point of creating an Assembly is not that you don't have to "copy it" to another project, but rather that you don't have to duplicate code.

Categories

Resources