Entity Framework - adding to auto generated table - sql-server

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; }
}

Related

Advanced NoSQL Query (RavenDB)

I'm trying to run a query that gets all of my references, but it isn't working.
What I have right now is
from UserGroups
where Id="ActionGroup"
select Accomplishments.ID, Accomplishments.Accomplish
But I need only the Accomplishments.Accomplish that belong in my other collection ActivityAccomplishments and these are nested in another object.
To be exact, I'm trying to figure out how to query the UserGroups collection and only look at the one with id="ActionGroup". After that I need all of the Accomplishments.Accomplish strings within the UserGroup list to be filtered out if they don't match a id in ActivityAccomplishment.
Basically, in the UserGroup I'm looking at it's List Accomplishments needs to filter out all strings within the Acc class that don't match an Id in ActivityAccomplishments. Can someone please help me.
Here are the classes I'm using.
public class UserGroups
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Acc> Accomplishments { get; set; }
}
public class Acc
{
public string Id { get; set; }
public List<string> Accomplish { get; set; }
}
public class ActivityAccomplishments
{
public string Id { get; set; }
}
try this:
from UserGroups
where Id = "ActionGroup" AND Accomplishments[].Accomplish != "theIdYouDontWant"
select Accomplishments[].Accomplish as AccomplishStringsList
(not necessary to add the 'as AccomplishStringsList' - it is just a name for the results)

Stored procedure to get related elements

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!

Update 2 tables from Edit Action (ViewModel).

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

Questions About Database

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; }
}

EF Code First: Many-to-many and one-to-many

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.

Resources