Got a table in SQL Server with a ClientId (int, foreign key). Run the program, everything fine.
Renamed the column to ClientID (capitals ID). After importing the model ASP.NET MVC Core
Scaffold-DbContext "Server=DESKTOP-XYZ;Database=MyDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force
and run the program, I get the error Invalid column name 'ClientId1'.
In the whole project ClientId1 is not found
Rebuild does not work
Adding ClientId1 (temporary) to the database will give an error about 'ClientId2', etc. ...
Drop create database won't work
Exit/Start VS2017 has no effect
SQL Server profiler shows a statement with 'ClientId1'
Get the error on a simple statement (for the table I made the change):
var projectList = await context.Project.ToListAsync();
The SQL Server profiler shows
SELECT [p].[ID], [p].[ClientID], [p].[ClientId1], [p].[Description], [p].[ExpirationDate], [p].[ImageDirectory], [p].[Name], [p].[Price], [p].[Size], [p].[StartDate], [p].[StatusID], ...
FROM [Project] AS [p]
The (correctly) generated Project class looks like (there is no ClientId1):
...
public int Id { get; set; }
public int? ClientId { get; set; }
public string Description { get; set; }
public DateTime ExpirationDate { get; set; }
public string ImageDirectory { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int Size { get; set; }
public DateTime StartDate { get; set; }
public int StatusId { get; set; }
...
How to solve this, thanks for any help!
The problem has nothing to do with the renaming of the column. In about the same period a property was added to the NOT generated partial Client class:
public partial class Client
{
...
public List<Project> ProjectList { get; set; }
...
}
This seems to confuse Linq. Needed is an extra [NoMapped] DataAnnotation. So the following code will work.
public partial class Client
{
...
[NoMapped]
public List<Project> ProjectList { get; set; }
...
}
Related
I am working on a code first .NET application that has an auto generated table. I created my Order class, with a link to Item. Instead of manually creating an OrderItem class, I simply used List<Item> and it auto generated the OrderItem table for me;
[Table("Orders")]
public class Order
{
[Key]
public int OrderId { get; set; }
public virtual List<Item> Items { get; set; }
public string OrderNumber { get; set; }
}
This, as expected has made a table in the database called OrderItems
Many years later, we now want to reference OrderItems from another table within entity framework. However, as it is not a class we can physically reference, we are not sure how to do it in code. Obviously changing the database is easy, but that won't help for code first.
I'd like to do something like the following
[Table("AnotherTable")]
public class AnotherTable
{
[Key]
public int AnotherTableId { get; set; }
public string SomethingHere { get; set; }
public virtual OrderItem OrderItem { get; set; }
}
Which I cannot do, as OrderItem isn't a class it understands.
Is my only alternative to try and recreate in code, what EF auto created for me? I.e
[Table("OrderItems")]
public class OrderItem
{
public virtual Order Order_OrderId { get; set; }
public virtual Item Item_ItemId { get; set; }
}
I need to write stored procedures in my project.
I need to get the data from stored procedures and not directly from tables.
Now I have these two models:
public class Make
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Model> Models { get; set; }
}
public class Model
{
public int ID { get; set; }
public string Name { get; set; }
public int MakeID { get; set; }
public string GroupName { get; set; }
public virtual ICollection<Engine> Engines { get; set; }
}
My question is if I can write a stored procedure that gets all the Makes from DB, and to get all the related Models for every make.
This is the stored procedure that gets all the makes:
CREATE PROCEDURE [dbo].[sp_GetAllMakes]
AS
SELECT
MFA_ID = CONVERT(INT, MFA_ID),
MFA_BRAND as Name
FROM
MANUFACTURERS
ORDER BY
MFA_BRAND
;
How can I modify this, to get the related model for every make? I have MODELS table in DB with MOD_MFA_ID as foreign key.
Thanks in advance!
I am trying to use computed column in my project, but I am facing the some error.
Column 'inserted.TotalMarks' cannot be referenced in the OUTPUT clause because the column definition contains a subquery or references a function that performs user or system data access. A function is assumed by default to perform data access if it is not schemabound. Consider removing the subquery or function from the column definition or removing the column from the OUTPUT clause.
Model
public partial class LMS_SurveyUser
{
public LMS_SurveyUser()
{
}
[Key]
public int SurveyUserID { get; set; }
public int SurveyID { get; set; }
public int UserID { get; set; }
public bool? IsCompleted { get; set; }
public DateTime? ExpiryDate { get; set; }
public int? MarksObtained { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public int? TotalMarks { get; set; }
public int LastPageAccessed { get; set; }
public int? WhoCreated { get; set; }
public DateTime? WhenCreated { get; set; }
public int? WhoModified { get; set; }
public DateTime? WhenModified { get; set; }
}
While creating the record with above mentioned it does not gives any error, but if I try to update the existing record then it gives the above mentioned error.
I have configured SQL function in the database for column 'TotalMarks'. I have tried with the fluent mapping also, but it gives the same error.
And in the 'TotalMarks' computed column specification I write this formula:
([dbo].[LMSFN_CalculateTotalMark]([SurveyID],[UserID]))
I am inserting/updating record like below,
Inserting Record
public void InsertSurveyUserDetails(LMS_SurveyUser model)
{
dbcontext.Add(model);
dbcontext.SaveChanges();
}
Updating Record
public void UpdateSurveyUserDetails(LMS_SurveyUser model)
{
dbcontext.Update(model);
dbcontext.SaveChanges();
}
Any help on this appreciated !
I write chat in C# with Entity Framework
This is my code
public class User
{
[Key]
public long id { get; set; }
public List<UserMessages> userMessages { get; set; }
}
public class UserMessages
{
[Key]
public long Id { get; set; }
public long ChatMateId { get; set; }
public string text { get; set; }
public DateTime? dateTime { get; set; }
}
Entity Framework code-first forced me to put on the Id field in UserMessages class attribute [Key] , I don't need this field at all because UserMessages is weak entity that will point to id of User
Can I delete long Id from sql table after ef create the table, without any problems?
No, you can't. EF requires that all tables have a [Key] on them, or it will blow up. Even when mapping to SQL Views (as I've learned the hard way, believe me).
In your case, you should actually be using a proper Foreign Key in that table also, like so:
public class UserMessages
{
[Key]
public long Id { get; set; }
[ForeignKey("User")]
public long UserId { get; set; }
public long ChatMateId { get; set; }
public string text { get; set; }
public DateTime? dateTime { get; set; }
public User User { get; set; }
}
public class Employee {
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public decimal Salary { get; set; }
public string Email { get; set; }
}
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
}
When I'm adding Data Annotation [Required(ErrorMessage = "Employee Name is required")] to the Name property it's throwing an InvalidOperationException. As I was trying to fix the bug I'm getting these suggestions online:
It means one of your classes use in the EmployeeContext has changed, but the database hasn't been updated so is now out of date. You need to update this use Code First migrations.
When I'm making the following changes its throwing an error now
public class Employee {
[Key]
public int Id { get; set; }
[DisplayName("Employee Name")]
[Required(ErrorMessage = "Employee Name is required")]
[StringLength(35)]
public string Name { get; set; }
public string Address { get; set; }
public decimal Salary { get; set; }
public string Email { get; set; }
}
Snapshot:
Questions:
If a Database Table is created is it possible to change a column ?
When adding a Data Annotation Attribute it's throwing an Exception, why is the database table column not changing ?
Addicted to your tutorials now
User Migration for update database structure
Without [Required] filed Name allow null (varchar(x) null), with [Required] Name change not null (varchar(x) not null)
If in database threre are rows with nullable Name, can be error on update (with migration)