I am writing project on ASP.NET MVC 4. The database for this project was not originally designed, and the modification or addition of new tables in the database is done campaign work. I use ADO.NET EF DatabaseFirst. During the work I needed additional properties to store information that I have ordered in the class with the fields of the database.
After that, I need to add a new table, but all of my follow-up in class with fields disappeared after the model update ADO.NET. Who knows how to safely modify my model no change of work already done?? Thanks in advance.
Related
I am trying with the help of Entity Framework to set up this without having to deal with the code-related part of SQL.
I created a model and added a migration via package manager console and it all worked well it updated and created the table.
The thing I want to ask is how does the entity know which migration I want to add.
I used:
add-migration (and put here the name of the migration file)
But the thing I don't understand is how does it know which model I want for my table?
Or put it in other words if I would have 2 models before I did any migrations which model would get chosen?
Would really appreciate it if someone could help me out.
Thanks in advance
Seems you are using entity framework migrations and got confused how it works. Here is the explanations:
Question: But the thing I don't understand how does it know which model I want for my table?
If you look into your project folder there is the directory
Migrations. Inside it all the migrations history logs written
into.When we made any changes on data model, EF Core compares the current model against a snapshot of the old model to determine the
differences, and generates migration source files; the files can be
tracked in your project's source control like any other source file.
Once a new migration has been generated, it can be applied to a database in various ways. EF Core records all applied migrations in a
special history table, allowing it to know which migrations have been
applied and which haven't
Question: If I would have 2 models before I did any migrations which model would get chosen?
As said earlier, as it keep track previous migrations history, so in your old model it compares the differences and overrite latest
changes that were not written on older files. This is how it works.
Hope above explanations guided you accordingly and redeem your confusions. You can also have a look on official documents here
I am developing an application with Asp.Net core 5 and the application accesses and displays information from two different databases(both Sql). One database is application’s own database where all the information will be stored/added. But there is another database already exists(on-prem server) and being used by another application. I want to read some master data from this database to use in my application. So I want to connect to the second database to just read master data and display it in application pages. For the first database connectivity I have created a separate entity project using Entity Framework Core 5.0 Code first approach.
How do I access second database just for read data purpose, Which would be the feasible approach for this. I was thinking to create another entity project with EF Core 5 DB first approach, but with this approach it creates DBContext class and all DbSets objects for each table. Which is not required I feel because I just want to read 8-10 tables from the entire database
Can anyone please suggest which would be a better approach for this? DB first approach or Ado.Net Vanilla method?
Finally I am going to reference these entity projects into my Web API application for all DB operations.
Thanks!
Within the same project, next to your existing dbContext, register a new extension of db context that will encapsulate the second database that you want to read from.
Do not copy the entire project from the start, rather think that the only reason to have a second dbContext to start with, is when dealing with multiple databases.
The rest of your code can remain re-usable ( probably ) and you dont need to cope with an other project as well
EDIT:
For example you might register your current context like below:
services.AddDbContext<ApplicationDbContext>(
options => options.UseSqlServer("name=ConnectionStrings:DefaultConnection"));
You can create a new class, lets say
public class OldDataDbContext : DbContext
{
public OldDataDbContext(DbContextOptions<OldDataDbContext> options)
: base(options)....
....
....
on that new dbContext class, register all the dbSets that you are going to read from that master db.
Then register that "new implementation of db context" with the connection string for the other database.
services.AddDbContext<OldDataDbContext>(
options => options.UseSqlServer("name=ConnectionStrings:OldDatabaseConnection"));
Now next to your DefaultConnectionString appSetting ( or however else you might have called it, add an other line with the other connectionString, with keyName "OldDatabaseConnection" and you can use that new class just as you use the old one.
I have Umbraco 7 website with MVC.
I want to perform some custom action on the database.
As I understand I should be using DbContext to connect.
I have referenced System.Data.Entity to get to DbContext class. However when I'm trying to use DbContext I'm getting an error saying
The type or namespace name 'DbContext' could not be found (are you missing
a using directive or an assembly reference?)
In my models namespace:
public class umbracoDbDSN : DbContext
{
//some code
}
Can you let me know what I am missing?
Thanks
You are mixing things up. Umbraco uses PetaPoco as ORM, not entity framework. You don't need to include the System.Data.Entity. Neither you need the DbContext.
However, if you have existing DataLayer logic which you need to incorporate, for legacy systems, you might need to continue with your code above. Then look for entity framework tutorials on the internet to continue your journey.
If you are not dragging legacy stuff, then the question is: do you want to perform queries on custom tables or do you want to query the Umbraco tables for some reason.
Let's start with the last one. Querying the umbraco tables:
If you want to connect to the umbraco SQL tables, I start wondering why. There is a ContentCache, which is blasting fast, and it enables you to query very quickly everything you need from the content section. You have API's for relationships, media, users, members and everything you need. So the question remains, WHY would you ever connect to the umbraco tables.
However, if you want to store data in custom tables, I would read this article of Warren: http://creativewebspecialist.co.uk/2013/07/16/umbraco-petapoco-to-store-blog-comments/
The idea is simple, you reuse the existing code base to extend umbraco behaviour without storing stuff in the content section.
Below a simple example for reusing the databaseconnection while querying some proper created table...
var db = ApplicationContext.Current.DatabaseContext.Database;
// Fetch a collection of contacts from the db.
var listOfContacs = db.Fetch<Contact>(new Sql().Select("*").From("myContactsTable"));
I am looking for a way to map existing tables in a project with the Eloquent ORM and use them in code. I use a MySQL database and plan to migrate to MSSQL. Any way points are appreciated.
You'll have to do this manually.
i.e., create an eloquent model for each of the tables you want access to in your code using eloquent.
If you don't have timestamps named created_at and updated_at, in your model you can disable those columns.
Manually
If you have a users table you could 'map' it with a user.php file in your models folder like this
class User extends Eloquent {
protected $table = 'users';
public $timestamps = false;
}
Via artisan
You can use Jeffrey Ways Laravel Generators to help streamline the initial creation of your models, however you'll still need to make the timestamp modification manually.
This looks like an old post, but it was edited a couple of days ago, so I don't know if the original author is looking for a solution again, but if someone needs this info, here is a packagist package for Laravel 5 to do what you are asking.
Laravel 5 model generator from existing schema:
https://packagist.org/packages/ignasbernotas/laravel-model-generator
Hope that helps someone!
There is also a Eloquent Model Generator library. It can be used for generating Eloquent models using database tables as a source. Generated model will include relation methods, docblocks for magic field and relations and several additional properties.
Another here: https://github.com/Xethron/migrations-generator.
You'll only want to use these generators for local development, so you don't want to update the production providers array in config/app.php. Instead, add the provider in app/Providers/AppServiceProvider.php.
For more details look here - https://packagist.org/packages/ignasbernotas/laravel-model-generator#user-content-installation
You can also use SQL Server Migration Assistant (SSMA) to port the database to SQL Server, but you will still need to write your own models to match the schema.
http://blogs.msdn.com/b/ssma/
http://www.microsoft.com/en-us/download/details.aspx?id=43688
Still this might help get halfway there, from both sides of the puzzle.
I'm new to Drupal 7. Right now I'm trying to use D7 to build an interface that allows me to directly manipulate the tables in the database.
I have installed a couple modules such as Data, Migrate, Feeds, and etc. I managed to create a view to display the table that I created using Data Module, but I need to add and update the rows from the interface.
Furthermore, is it possible to set up relational tables so that I can update or delete related rows at the same time using Drupal?
Thanks in advance
Using the data module that is quite easy because it comes with the "Data Entity" submodule that creates an entity form for you. This allows you to update data table rows. It should add an edit button at the end of your data view for the table.
Using some basic custom code you can always create a form to add/update data to the table as well. For form creation please have a look at:
http://api.drupal.org/api/drupal/includes%21form.inc/group/form_api/7