I have added a (bit\bool) column IsController to one of my tables
ALTER TABLE P_USER ADD IsController bit NOT NULL DEFAULT 0
Updated the edmx and added IsController to the MY_USER entity, then changed its name to
IsControllerX and mapped it to the IsController from the table.
And set this inside the solution.domain.business cs file:
public virtual bool IsControllerX { get; set; }
On debug I have the error:
An error occurred while executing the command definition. See the
inner exception for details. InnerException: Invalid column name
'IsController'.
The error is with the IsController and not with IsControllerX! That is the name of the column that I mapped to - the name of the column inside the database!
Can someone please explain why I get this error?
HOW did you update your model and your generated classes??
In the EDMX, you should select the Update Model From Database option from the context menu, and then in the Update Wizard, you should pick your table that has been modified:
Doing this will properly update your EDMX and it works just fine in my scenario. Did you do it this way, and it still fails?? Or did you just do it manually ??
Related
Using Postgres and Spring Boot. Running into the following error when I try to add the following column and data to my table.
psql:resorts.sql:51: ERROR: invalid input syntax for type smallint: "https://someurl.com/that-im/trying-to-add/"
LINE 24: 'https://someurl.com/that-im/trying-to-add/'
Where I add in the column in my Spring Boot Model the column above is a short but I clearly define this column as a String as seen here:
#Column(name = "things")
private short things;
#Column(name = "item_url")
private String itemUrl;
My sql file where I insert the data into the database looks like this:
INSERT INTO table (
...,
things,
item_url
) VALUES (
...,
5,
'https://someurl.com/that-im/trying-to-add/'
),(
When I remove the column for item_url and remove it and the url from the SQL file everything works, but as soon as I put them back in I get the error above. Any help would be appreciated.
What ended up being the case was that when the table in my database was initially created I had accidentally labeled the type for the item_url as a short. Despite changing it in the SB model file to a String it still wouldn't take on the table.
From there I attempted to DROP TABLE IF EXISTS which deleted my table but for some reason would not allow to re-create a table with the old name. So I went into the SB model file and SQL seeder file and changed the name of the table to something else. From there it was able to create a a new table with all of the correct data and types with no errors.
I have a custom table naming convention which prefixes B_ to the entity name. This works fine when I generate the migration scripts. However, when I execute any get method, the EF Core generated code doesn't generate the table name with the prefix. Instead it uses the Entity name itself as table name and due to this, an exception - *
Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid object name 'Appointment'
*.- is thrown.
The query is supposed to be generated with table name as B_Appointments, which is the custom table name.
Do anyone have any idea, why this is happening?
I have added this computed column inside my data model
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string FullName { get; private set; }
After that I created it inside my database using this query
ALTER TABLE [MyDataBase].[dbo].[User] ADD FullName as ([FirstName] + ' ' + [LastName])
When I run my code I get an error that my database has changed .
My question How to create migration for this computed column (because it's already created using sql query)
Entity Framework doesn't know how to properly handle migrations for computed columns, so you need to help it out.
Firstly, delete the computed column from the table in the database.
Then create a new migration in the package manager console:
add-migration FullNameComputed
Replace the body of the Up() method in the new migration with the following:
Sql("ALTER TABLE [TableName] ADD [FullName] AS ([FirstName] + ' ' + [LastName])");
Finally, run the migration from the package manager console:
update-database
I try to change MaxLength property from 100 to 50 and i got exception that Says
"The index 'IX_Singers_Name' is dependent on column 'Name'.
ALTER TABLE ALTER COLUMN Name failed because one or more objects access this column."
Mode is :
public class Singer : NamedEntity
{
[MaxLength(50)] // It was 100
public override string Name { get; set; }
}
As i understand, entity framework needs to alter table for this change but it can't alter table because an index exist on Name property. So how i can make it possible with entity framework migrations ?
I can possibly drop index in migration then change maxlength in next migration and create index the last migration again. But i believe that there should be exist an easy way to change that attribute value.
In SQL Server, indexes are pretty much like tables themselves. So if you've got the column in an index, both that index and the table would need to be modified. I agree that where EF migrations were scaffolded to add the index (e.g. for a foreign key) they should also take care of removing and reapplying the index. However, in this instance the index would have had to have been added manually. Therefore it will need to be maintained manually in the migration. Note that it can be done in a single migration:
public override void Up()
{
DropIndex("dbo.Singer", new []{"Name"});
/* Code to alter the table */
CreateIndex("dbo.Singer", "Name");
}
Don't forget to put this in both the Up() and Down() methods.
I'm trying to use migrations to add a parent table to an existing child table. For eg. I currently have User table, now I want to add a Department table that has a 1 to many relationship: Department has many User.
My questions, in automatic update, can I somehow seed the parent table before adding the FK so I can update all the children to this default seeded Department? If automatic update cannot do this, how do I accomplish this in code?
What I currently did: Made the FK nullable, created the Parent and seeded it, then update all child User FK to the parent. But now I cant change the FK not nullable because throws this error: Automatic migration was not applied because it would result in data loss.
Switching from nullable to non-nullable is considered data loss because after the migration, there is no way to tell which rows (if any) were null. If you are ok with this, you can call Update-Database with the -Force flag.
Another option would be to add a code-based migration that would:
Add the Departments table
Insert a default department
Add the required FK column to User with a default value of the inserted department