I have an old Oracle Database (11.2.0.4.0) that I would like to scaffold using EF Core 6.4.4 and Oracle.EntityFrameworkCore 6.21.61.
The scaffolding command I use is:
Scaffold-DbContext "User Id=User;Password=Pass;Data Source=Inst;" Oracle.EntityFrameworkCore -OutputDir Models -Context MyTestContext -ContextDir . -force -Tables Table1, Table2 ...
It does a great job about the entities themself, but it does not recognize the relations between tables.
When scaffolding is done, I can set compatibility through UseOracleSQLCompatibility, but I am not aware of a way to tell scaffolding about compatibility.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.LogTo(message => Debug.WriteLine(message));
optionsBuilder.UseOracle("User Id=User;Password=Pass;Data Source=Inst;",
b => b.UseOracleSQLCompatibility("11"));
}
}
When doing a
var master = db.MasterEntities.Where(m => m.MasterId == 1)
.Include(m => m.Details).ToList();
it returns a list with one master, all properties correctly filled, but with no details.
The SQL debug info sent to the debug window only is about querying the master table.
The problem here was not the scaffolding.
The problem was .Include
There was a
using System.Data.Entity;
where there needed to be a
using Microsoft.EntityFrameworkCore;
Related
I want two different containers in my Cosmos DB database to contain the exact same data all the time. The only difference will be the partition key of the container.
What is the easiest way to accomplish this? I am looking for a method with none to little code so I do not like the Data Factory solution that the internet seems to recommend. Perhaps there is another service in Azure or a third party service - or maybe it can be done robustly with simple triggers?
EDIT: Clarification - I need them to be continuously updated. One container is where all the data is changed during normal use and the other container should be kept synchronized as it happens.
This is the function code for it:
using System;
using System.Collections.Generic;
using Microsoft.Azure.Documents;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace CosmosDBSyncFunction
{
public static class SyncCosmosDb
{
[FunctionName(nameof(Sync))]
public static void Sync(
[CosmosDBTrigger(
databaseName: "evaluation",
collectionName: "lorem",
ConnectionStringSetting = "cosmos-mm-eval",
LeaseCollectionName = "leases",
CreateLeaseCollectionIfNotExists=true
)]IReadOnlyList<Document> input,
[CosmosDB(
databaseName: "evaluation",
collectionName: "ipsum",
ConnectionStringSetting = "cosmos-mm-eval")] IAsyncCollector< Document> output,
ILogger log)
{
foreach(var item in input)
{
output.AddAsync(item);
}
}
}
}
and the settings which has to be configured
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"cosmos-mm-eval": "secret",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
If you don't want to write your own code to do this you can use this repo which has the Cosmos DB Live Data Migrator.
This can be deployed via Azure deploy button. Once deployed you can open the website and enter all of the required information. Then click another button and it will keep the two containers in sync with different partition keys.
Hi Could you please assist,i have run a scafold from my database using the below PM command and it works fine and the DB context is created.
Scaffold-DbContext "Data Source=(localhost);Initial Catalog=tumoDB;User ID=tumo;password=****" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Infrastructure\Models -Tables BnplIndicator -f
But when i try to follow .Net security principles by placing th the connectionstring in appsettings.json,i get the error in the title that the name of my DB(tumoDB) is not found,please see my connection strings bellow.
"ConnectionStrings": {
"MySql": {
"PhoneAppDB": {
"ConnString": "Server=(localhost)\Sql1;uid=tumo;pwd=****;Convert Zero Datetime=True;",
// "ConnString": "Server=(localhost)\Sql2;port=3030;Database=MohemStores;user=root;password=***;",
"Timeout": 60000,
"MaxRetries": 6
}
},
"SqlServer": {
"tumoDB": {
"ConnString": "Data Source=(localhost);Initial Catalog=tumoDB;User ID=tumo;Password=***;",
"Timeout": 60000,
"MaxRetries": 5
}
}
},
...
and now i use this below i get the error:tumoDB was not found in application configuration
Scaffold-DbContext -Connection Name=tumoDB Microsoft.EntityFrameworkCore.SqlServer -OutputDir Infrastructure\Models -Tables BnplIndicator -force
I researched and even added the code in the Iconfiguration in Startup.cs as recommended but still getting the same error,please note all required packages (EF.Sqlserver and EF.tools) are installed properly and the API runs it is just the command to scafold DBcontext using the name that gives problems.
services.AddCustomDataStoreConfiguration(Configuration);
services.AddDbContext<tumoDBContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("tumoDB")));
I actually found what the answer was,you cannot create a sub connection string under another connection string.
Solution:
"ConnectionStrings": {
"PhoneAppDB":"Server=(localhost)\Sql1;uid=tumo;pwd=****;Convert Zero Datetime=True;",
"tumoDB":
"ConnString": "Data Source=(localhost);Initial Catalog=tumoDB;User ID=tumo;Password=***;"
},
I installed recently laravel 5.7. How to fix error "php artisan migrate"?
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))
at C:\laravel\blg2\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error 661| // message to include the bindings with SQL, which will make this exception a 662| // lot more helpful to the developer instead of just the database's errors. 663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes") C:\laravel\blg2\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
2 PDOStatement::execute()
C:\laravel\blg2\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
Please use the argument -v to see more details.
Thank you
Laravel uses the utf8mb4 character set by default, which includes support for storing "emojis" in the database. If you are running a version of MySQL older than the 5.7.7 release or MariaDB older than the 10.2.2 release, you may need to manually configure the default string length generated by migrations in order for MySQL to create indexes for them.
Inside your AppServiceProvider:
use Illuminate\Support\Facades\Schema;
...
public function boot()
{
Schema::defaultStringLength(191);
}
Alternatively, you may enable the innodb_large_prefix option for your database. Refer to your database's documentation for instructions on how to properly enable this option.
For more information about indexes: https://laravel.com/docs/5.7/migrations#indexes
If you are running a version of MySQL older than the 5.7.7 release or MariaDB older than the 10.2.2 release, you may need to manually configure the default string length generated by migrations in order for MySQL to create indexes for them.
You can have it fixed as Chin Leung said.
Or adding the length of the record directly in the migration.
$table->string('email', 100); VARCHAR equivalent column with a optional length.
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 50);
$table->string('email', 90)->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
On xampp php 7.3.2, laravel 5.7 & also in laravel 5.8.3 it happened to me. Only changed the config/database.php file.
config/database.php
Changed charset & collation on mysql & it worked fine. Change this part
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
as
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
Now it will accept the long keys.
You need to go to
App\Providers\AppServiceProvider.php
Now Add the following code and then save and run php artisan serve
use Illuminate\Support\Facades\Schema;
...
public function boot()
{
Schema::defaultStringLength(191);
}
I use Laravel 5.7 but this error always comes up. ADD this code in every project. Also try to use Laravel with php7+ so that it doesnot show you PDO::Exception Error...
Open your AppServiceProvider.php, Path >> App\Providers\AppServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
**use Illuminate\Support\Facades\Schema;**
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
**Schema::defaultStringLength(191);**
}
You can define the string length as well to solve this kind of error
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name',20);
$table->string('email',80)->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password',8);
$table->rememberToken();
$table->timestamps();
});
I am trying to configure Audit.net and define my custom logic for saving logs.
Is there a way to configure included entities within context?
I tried this
`
public ResidentMasterContext(DbContextOptions options) : base(options)
{
AuditDataProvider = new DynamicDataProvider();
Mode = AuditOptionMode.OptIn;
IncludeEntityObjects = true;
EntitySettings = new Dictionary<Type, EfEntitySettings>
{
{typeof(Apartment), new EfEntitySettings()}
};
}
`
but OnScopeSaving is not firing. And when I change mode to OptOut it takes all entities
I guess you are referring to the Audit.NET EntityFramework extension.
if you use OptIn you need to mark the included entities with [AuditInclude] attribute, or use the Include methods of the fluent API. You can check the documentation here.
An example using the fluent API for the EF configuration, to include only the entities User and UserDetail:
Audit.EntityFramework.Configuration.Setup()
.ForContext<ResidentMasterContext>(config => config
.IncludeEntityObjects())
.UseOptIn()
.Include<User>()
.Include<UserDetail>();
An example of the output configuration:
Audit.Core.Configuration.Setup()
.UseDynamicProvider(_ => _.OnInsertAndReplace(auditEvent =>
{
Console.WriteLine(auditEvent.ToJson());
}));
I am getting a NullReferenceException on a project recently converted to dotnet core 2.1.
My EF library project is dotnet core 2.1. Code first migrations.
CsvHelper 6.0.2
Microsoft.EntityFrameworkCore 2.1.0
Microsoft.EntityFrameworkCore.Design 2.1.0
Microsoft.EntityFrameworkCore.SqlServer 2.1.0
Microsoft.EntityFrameworkCore.Tools 2.1.0
Output from the add-migration command
PM> add-migration -Name MyMigration -Context EscData01Context -Project Data\EscData01 -StartupProject PacmsTri -verbose
Using project 'Data\EscData01'.
Using startup project 'PacmsTri'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\temp\PacmsTri\dev\***\source\PacmsTri\bin\Debug\netcoreapp2.1\PacmsTri.deps.json --additionalprobingpath "C:\Users\***\.nuget\packages" --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\temp\PacmsTri\dev\***\source\PacmsTri\bin\Debug\netcoreapp2.1\PacmsTri.runtimeconfig.json "C:\Users\***\.nuget\packages\microsoft.entityframeworkcore.tools\2.1.0\tools\netcoreapp2.0\any\ef.dll" migrations add MyMigration --json --context EscData01Context --verbose --no-color --prefix-output --assembly C:\temp\PacmsTri\dev\****\source\PacmsTri\bin\Debug\netcoreapp2.1\EscData01.dll --startup-assembly C:\temp\PacmsTri\dev\****\source\PacmsTri\bin\Debug\netcoreapp2.1\PacmsTri.dll --project-dir C:\temp\PacmsTri\dev\****\source\Data\EscData01\ --language C# --working-dir C:\temp\PacmsTri\dev\****\source --root-namespace Sms.Office.Data.EscData01
Using assembly 'EscData01'.
Using startup assembly 'PacmsTri'.
Using application base 'C:\temp\PacmsTri\dev\***\source\PacmsTri\bin\Debug\netcoreapp2.1'.
Using working directory 'C:\temp\PacmsTri\dev\***\source\PacmsTri'.
Using root namespace 'Sms.Office.Data.EscData01'.
Using project directory 'C:\temp\PacmsTri\dev\***\source\Data\EscData01\'.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider...
Finding IWebHost accessor...
Using environment 'Development'.
Using application service provider from IWebHost accessor on 'Program'.
Found DbContext 'AuthDbContext'.
Found DbContext 'DmsContext'.
Found DbContext 'EscData01Context'.
Found DbContext 'MailServicesContext'.
Found DbContext 'PmsDdContext'.
Finding DbContext classes in the project...
Using context 'EscData01Context'.
Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 2.1.0-rtm-30799 initialized 'EscData01Context' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding design-time services referenced by assembly 'PacmsTri'.
No referenced design-time services were found.
Finding IDesignTimeServices implementations in assembly 'PacmsTri'...
No design-time services were found.
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.EntityFrameworkCore.Metadata.Internal.TableMapping.<>c.<GetRootType>b__10_0(IEntityType t)
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at Microsoft.EntityFrameworkCore.Metadata.Internal.TableMapping.GetRootType()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedProperties(TableMapping target)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(TableMapping target, DiffContext diffContext)+MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext()
at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IModel source, IModel target)
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.
PM>
I have tried adding a IDesignTimeServices object to my startup project, and tried adding a IDesignTimeDbContextFactory as well.
I also tried creating a new sample project with the same dotnet and EF versions numbers, pointing at same db and was able to generate the context and create the migration successfully. So there must be some difference between the two project I cannot see.
PM> add-migration MyMigration2 -Project ClassLibrary1 -StartupProject WebApplication20 -Context ESC_DATA01Context
Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 2.1.0-rtm-30799 initialized 'ESC_DATA01Context' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
To undo this action, use Remove-Migration.
Any help greatly appreciated.
Update:
I had originally posted that this error was caused by migration of the project to standard 2.0 but discounted that idea after converting it back to dotnetcore2.1 and error persisted.
Looks like this error was caused by some references to models in my ModelSnapshot class that did not exist anymore. Including some Identity models that were left behind after I removed AspNetCore.Identity from the Context.
Deleting the code below from my EscData01ContextModelSnapshot fixed the issue.
modelBuilder.Entity("Sms.Office.Data.EscData01.Models.ExcelTemplate", b =>
{
b.HasOne("Sms.Office.Data.EscData01.Models.File", "File")
.WithMany()
.HasForeignKey("FileId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.HasOne("Sms.Office.Data.EscData01.Authentication.ApplicationRole")
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.HasOne("Sms.Office.Data.EscData01.Authentication.ApplicationUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.HasOne("Sms.Office.Data.EscData01.Authentication.ApplicationUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.HasOne("Sms.Office.Data.EscData01.Authentication.ApplicationRole")
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade);
b.HasOne("Sms.Office.Data.EscData01.Authentication.ApplicationUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.HasOne("Sms.Office.Data.EscData01.Authentication.ApplicationUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});