i am developing a samll webapp with aspnetcore and efcore, created the project use the vs2017 defualt tempalte and replace with the efsql to efsqlite.
after create the table, i found that all datetime? are mapping to TEXT and not null.
```c#
// both are mapped with TEXT
[DataType(DataType.DateTime)]
[StringLength(100)]
public string basename { get; set; }
public DateTime? requirementdate { get; set; }
```
ps: just use the annotation(code first), most of the columns are mapped to TEXT type. below is the generated table example:
Related
Specify a Parent-Child relationship in EF Core without using identity columns
What's an efficient way within Entity Framework Core 5 (C#) to work with the data in a hierarchial table that is linked via non-identity columns.
Here's my primary class:
public class ServiceProvider
{
[Key]
public int Id { get; set; }
public string ParentSPCode { get; set; }
public string SPCode { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ContactEmail { get; set; }
public string Status { get; set; }
}
The SPCode value is unique, which I enforce via C# code. The ParentSPCode may be null or must match an existing SPCode. Again I enforce this via C# code.
I want this table to hold any number of levels of parent-child (1 or more) records, as defined by ParentSPCode-SPCode pairs.
I can retrieve these records via a complex hierarchy of LINQ "joins", but I am thinking there must be a cleaner way by defining the appropriate EF Core 5 relationship.
If I was in SQL Server, I would do this via a CTE.
I want to be able to bring in the child records in a manner similar to .Include(q => q.ParentSPCode == x.SPCode).
I am making a Web API .Net Core 3.1 application that serves data from a MySQL database with OData protocol.
My question is how can I add internationalization functionality (i.e. i18n) to a specific column without any foreign keys, by using JSON structure?
For example I would like to define an entity model like this:
public partial class City
{
public City(){}
public ulong Id { get; set; }
public string Name { get; set; }
public string CountryId { get; set; }
public DateTime? CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public virtual Country Country { get; set; }
}
and then the data in Name column would be stored like the following for en and de locales:
{"en":"Cologne","de":"Köln"}
And then when the current locale is de the Köln and when the en is set as the current locale then Cologne fetch in the time of reading data!
Also when we store the data, the passed string is stored in the relevant property in the JSON format. For example if the locale is set to es and user passes Kolne then we have to have the following data in the DB:
{"en":"Cologne","de":"Köln","es":"Kolne"}
What I am looking for is some solutions like this one which is suggested for Laravel and store the translations exactly in the way that I explained (i.e. in a JSON object and in a single column without any foreign keys)!
I am looking for some kind of NuGet that do this process automatically.
Please notice this question is not about i18n JSON files!!! and is about JSON objects in database!
Background
I have an existing dbset with a FooCol1 field that originally was a string data type.
public class Foo
{
[Key]
public Guid Id { get; set; }
public string FooCol1 { get; set; }
public string FooCol2 { get; set; }
public string FooCol3 { get; set; }
}
I want to change this to an int so I've carried out the following in EntityFrameworkCore:
Added a migration column and populated the migration column using SQL
Model
public class Foo
{
[Key]
public Guid Id { get; set; }
public string FooCol1 { get; set; }
public string FooCol2 { get; set; }
public string FooCol3 { get; set; }
public int? FooCol1Migration { get; set; } //new migration column
}
Migration
migrationBuilder.AddColumn<int>(
name: "FooCol1Migration",
table: "Foos",
nullable: true);
migrationBuilder.Sql("Update Foos set FooCol1Migration=1"); //script to populate the new migration column
Now the data is in the new column, remove the original property and rename the migration column.
Model
public class Foo
{
[Key]
public Guid Id { get; set; }
public string FooCol2 { get; set; }
public string FooCol3 { get; set; }
public int? FooCol1 { get; set; } //renamed from FooCol1Migration
}
Migration
The original migration scripts provided by EFC was to alter the data type of FooCol1 column and drop the FooCol1Migration column.
I changed this to do a rename instead so it kept the data I migrated in step 1.
migrationBuilder.DropColumn(
name: "FooCol1",
table: "Foos");
migrationBuilder.RenameColumn(
name: "FooCol1Migration",
newName: "FooCol1",
table: "Foos");
I think this is where my issue came from.
Issue
When I try and publish changes to my staging environment using Web Deploy, the SQL scripts are erroring on the line that does populates the migration column.
I've looked at the scripts generated and found it erroring here:
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20190212155305_Adding FooCol1Migration field to Foo')
BEGIN
update Foos Set FooCol1Migration=1
END;
GO
Obviously FooCol1Migration doesn't exist any more and even though it's wrapped in a NOT EXISTS, SQL server is still checking the underlying data structure and stopping the publish from completing.
Question
What's the best way to progress from here so that I can successfully publish my SQL scripts? I don't want to just add the FooCol1Migation column back in to allow the scripts to run.
The versions of software I'm using
Visual Studio 2017 15.7.4
Asp.Net Core 2.1.1
Okay, so I got this working by executing the data migration SQL using a stored procedure rather than running the SQL directly
migrationBuilder.Sql("Execute sp_executesql \"update Foos Set FooCol1Migration=1\"")
Now generates:
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20190212155305_Adding FooCol1Migration field to Foos')
BEGIN
Execute sp_executesql "update Foos Set FooCol1Migration=1"
END;
GO
If anyone has any better / alternative solutions or comments around my issue, I'd still be interested to hear them.
I've hit a snag while building a .net mvc site. I have 2 related objects and am struggling with properly linking them. Specifically:
public class Address
{
public int AddressId { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostCode { get; set; }
[ForeignKey("AddressCategory")] // <-- EF adds field to below object's table
public int AddressCategoryId { get; set; }
public virtual AddressCategory AddressCategory { get; set; }
}
public class AddressCategory
{
public int AddressCategoryId { get; set; }
public string Description { get; set; }
}
Adding the [ForeignKey] data annotation to the Address object results in EF adding an Address_AddressId column to the AddressCategory table, which I don't want (or need) to happen.
I've tried to omit the ForeignKey attribute, but then I run into other errors because .net can't link the tables (e.g. Unknown column 'Extent1.AddressId' in 'field list'). Additionally, I wouldn't be able to use:
var addresses = db.Addresses.Include(l => l.AddressCategory);
Is there any way to link the 2 tables without EF adding an additional column to the AddressCategory table?
Thank you to #cloudikka for responding. After much trial-and-error I seem to have gotten it to work simply by omitting any ForeignKey reference from either object. I let EF rebuild the database and perform all scaffolding (CRUD forms) and they have been created perfectly.
My take-away is that foreign key attributes should be used for parent-child relationships, but not for look-up tables. I clearly have much to learn about asp.net mvc!
I have couple of entity who inherits base class with common fields like below:
[DataType(DataType.Text)]
[StringLength(100)]
public string CreatedBy { get; set; }
[DataType(DataType.DateTime)]
public DateTime? CreatedDate { get; set; }
[DataType(DataType.Text)]
[StringLength(100)]
public string ModifiedBy { get; set; }
[DataType(DataType.DateTime)]
public DateTime? ModifiedDate { get; set; }
I would like to fill these fields right before it goes to contex. Is there some events/method/handler which I may reuse to do some actions with entity before it placed to context?
I would like these fields are filled at the time its added to context, not put to the database.
Solution: Entity Framework/SQL2008 - How to Automatically Update LastModified fields for Entities?
Not sure 100% what you mean by filling these fields to context before they go to the database.. but if you want to persist them at some point these fields should be part of an object (entity) and then you can populate the poco using the setter method. You can always call context.Save()/Update() methods when you want to.