I have following two model classes
public partial class Items {
public Items() {
this.Items_RATINGS = new HashSet<Items_RATINGS>();
}
public int ITEMID { get; set; }
public string ITEMNAME { get; set; }
public virtual ICollection<Items_RATINGS> Items_RATINGS { get; set; }
}
public partial class Items_RATINGS
{
public int ItemsID { get; set; }
public byte ItemsRATING { get; set; }
public string COMMENTS { get; set; }
public virtual Items Items { get; set; }
}
I've also created stored procedure to review average rating
CREATE PROCEDURE DBO.GET_ITEM_AVERAGERATING
#ITEMID INT
AS
BEGIN
SELECT AVG(OVERALLRATING) AS OVERALLRATING FROM ITEMS_RATINGS WHERE ITEMID = #ITEMID GROUP BY ITEMID
END
GO
In model class, I have added
public virtual ICollection<GET_ITEM_AVERAGERATING_Result> GET_ITEM_AVERAGERATING { get; set; }
In the controller calls, I tried adding
return View(db.Items.Include(c => c.GET_ITEM_AVERAGERATING).ToList());
However, I'm not getting the value. Its throwing an error.
You need to add the stored procedure to your edmx model
Here is how to do it
1- Open your edmx file
2- Next right click anywhere on the Edmx designer and choose Add > Function Import.
3- This will bring up the wizard for adding Function. Choose the procedure you want to add and choose the return type .
4- Then you can call your stored procedure as a CLR method from your dbcontext.
Related
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; }
...
}
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 want first to say sorry, because I'm new to this programming language, so forgive me if I say or do something wrong.
I created a project with 3 class libraries (1 of them contains the tables from sql server). I made a ViewModel based on the tutorial from: "http://tutlane.com/tutorial/aspnet-mvc/how-to-use-viewmodel-in-asp-net-mvc-with-example", and now I want to be able to update the data in those tables, but I don't know how. I tried to do something but it failed.
`namespace BOL2.ViewModel
{
public class NIRIO
{
[Key]
public int ID { get; set; }
public int Number { get; set; }
public Nullable Date { get; set; }
public int NirID { get; set; }
[DisplayName("TypeID")]
public int TipID { get; set; }
public int SupplierID { get; set; }
public int ProductID { get; set; }
public Nullable<System.DateTime> EntryDate { get; set; }
public Nullable<System.DateTime> ExitDate { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
public decimal Total { get; set; }
public BOL2.tbl_NIR tbnir;
public BOL2.tbl_I_O tblio { get; set; }`
This is my ViewModel. It contains data from those 2 tables (tbl_NIR, first 3, and the others from tbl_I_O. I saw something on my research that they had a repository class, but I don't now if I should do another class for the viewmodel or I sould use the 2 that I already have? Any help is greatly appreciated.
You could do something similar to this.
public void UpdateCar(CarViewModel viewModel)
{
using (DataContext context = new DataContext())
{
CarEntity dataModel = context.CarEntities.where(x => x.Id == viewModel.Id).First();
dataModel.Name = viewModel.Name;
dataModel.Type = viewModel.Type;
context.SaveChanges();
}
}
You need to create your model objects from your view model and set the values for the models.
I'm not quite sure what is the method you used and didn't work. Just in case you tried to update the tables through the view, then you cannot do that. Because you have join
inform IT explanation
I recommend you to create a stored procedure in your database. Then call the procedure trough the code. It's secure and fast. explained here