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!
Related
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
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; }
}
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.
This is probably just because my knowledge with the EF Code First fluent API is lacking, but I'm stumped.
I want to model the following:
A Groups collection with Id and Name
A Users collection with Id and Name
Each user is assigned to exactly one primary group
Each user may have zero or many secondary groups
The table structure I'm going for would look like:
Groups
Id
Name
Users
Id
Name
PrimaryGroupId
SecondaryGroupAssignments
UserId
GroupId
I've been beating my head against a wall trying to model this with EF Code First, but I can't get it to accept both relationships between User and Group. Sorry for not posting any .NET code (I'm happy to), but it's probably all wrong anyway.
Is there a way to make EF model this? I'm assuming I have to do some sort of configuration with the Fluent API. Maybe a better question is: is there any good, definitive reference for the Fluent API?
Thanks!
Try this (untested):
public class Group
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<User> PrimaryUsers { get; set; }
public virtual ICollection<User> SecondaryUsers { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int PrimaryGroupId { get; set; }
public virtual Group PrimaryGroup { get; set; }
public virtual ICollection<Group> SecondaryGroups { get; set; }
}
public class Context : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Group> Groups { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>()
.HasRequired(u => u.PrimaryGroup)
.WithMany(g => g.PrimaryUsers)
.HasForeignKey(u => u.PrimaryGroupId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<User>()
.HasMany(u => u.SecondaryGroups)
.WithMany(g => g.SecondaryUsers)
.Map(m => m.MapLeftKey("UserId")
.MapRightKey("GroupId")
.ToTable("SecondaryGroupAssignments"));
}
}
Based on Ladislav's excellent answer, here's how to do it without using any mappings - just attributes applied to the Model classes themselves:
public class Group
{
public int Id { get; set; }
[MaxLength(300)]
public string Name { get; set; }
public ICollection<User> Users { get; set; }
}
public class User
{
public int Id { get; set; }
[MaxLength(300)]
public string Name { get; set; }
[ForeignKey("PrimaryGroup")]
public int PrimaryGroupId { get; set; }
[Required]
public Group PrimaryGroup { get; set; }
[InverseProperty("Users")]
public ICollection<Group> SecondaryGroups { get; set; }
}
Notes
If you want, you can add the virtual keyword to the 2 ICollections and the Group. This allows lazy-loading. Performance-wise, I don't recommend it, but it is possible.
I included MaxLength attributes with an arbitrary (but safe) length of 300, because putting strings out in EF without a MaxLength gets you low-performance NVarChar(MAX) columns. Totally irrelevant to what's being asked but better to post good code.
I recommend against class names "User" and "Group" for your EF classes. They're going to complicate any SQL you attempt to run later, having to type [User] and [Group] to access them, and complicate using these classes in MVC Controllers where your class User will conflict with the Context property User that gives you access to the Asp.Net Identity library.