I am trying to call a public virtual into a Asset.cs to link data on the ModelID.
My issue is that the Model.cs is outside of the area of where the asset.cs is.
How do i link the two tables models together when they are separated by areas ?
Assets.cs in a area, using "model" is an issue in the public virtual because it cant find it in the Area.
namespace Assets.Areas.Company.Models
{
public class Asset
{
[Key]
public int AssetID { get; set; }
public int ModelID { get; set; }
public virtual Model ModelName { get; set; }
Model.cs outside the area
namespace Assets.Models
{
public class Model
{
[Key]
public int ModelID { get; set; }
public int AssetAssignmentID { get; set; }
[Required]
[StringLength(100, MinimumLength = 2)]
public string ModelName { get; set; }
I have tried a viewmodel approach but it just adds the fields to the asset table rather than foreign keys when i use public virtual ModelVM ModelName { get; set; }
I have tried using the using Assets.Models; to link to the model.cs when looking at public virtual Model ModelName { get; set; } , but when updating the table an object named 'models' is already in the database.
I have found i can you models outside of an area, with:
using Assets.Models;
My EF Migration was causing me the issue with "object named 'models' is already in the database." This was because the table Models was already there!
Related
I want to add two properties from the city model:
after migration this error shows up:
Unable to determine the relationship represented by navigation
'City.Orders' of type 'ICollection'. Either manually configure
the relationship, or ignore this property using the '[NotMapped]'
attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
here is my code :
public class Order
{
public virtual City FromCity { get; set; }
public virtual City ToCity { get; set; }
}
public class City
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
I suppose your model is more complicated than just FromCity and ToCity because I don't think it's a good idea to store such information in a different table. Yet, You can use inheritance in this case.
The table-per-hierarchy (TPH) pattern is used by default to map the inheritance in EF. TPH stores the data for all types in the hierarchy in a single table.
However, for your scenario, you can have a base class that holds all related attributes.
public class CityBase
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
}
Then suppose you need two entities as per your scenario:
public class FromCity : CityBase
{
public virtual ICollection<Order> Orders { get; set; } = null!;
}
public class ToCity : CityBase
{
public virtual ICollection<Order> Orders { get; set; } = null!;
}
And the order entity:
public class Order
{
public int Id { get; set; }
public string OrderTitle { get; set; } = string.Empty;
public virtual FromCity FromCity { get; set; } = null!;
public virtual ToCity ToCity { get; set; } = null!;
}
This approach can solve your problem with a One-to-Many relationship between Orders and FromCity, ToCity as per below diagram:
I'm creating an EF6 based application with Visual Studio 2013. I used the Model First approach, and after creating my data model in the editor, I can see and use the classes generated for my entities. But when following the documentation I tried to create a new instance of an entity with the static method CreateObjectName that is supposed to be autogenerated for each of the data model entities, I discovered that the autogenerated entities do not have such static methods, I can only create new instances with the new keyword. Here is the autogenerated code of one of my entities:
using System;
using System.Collections.Generic;
public partial class Movement
{
public int IdMovement { get; set; }
public MovementType Type { get; set; }
public decimal Amount { get; set; }
public Nullable<decimal> TargetAmount { get; set; }
public string Description { get; set; }
public System.DateTime Date { get; set; }
public int IdTag { get; set; }
public Nullable<int> IdContainerSource { get; set; }
public Nullable<int> IdContainerTarget { get; set; }
public virtual Tag Tag { get; set; }
public virtual Container ContainerSource { get; set; }
public virtual Container ContainerTarget { get; set; }
}
I have T4 as the Code Generation Strategy, so according to the documentation I should have those methods. I have tried regenating the code, with no luck. I think that the CreateObject static method is cleaner that using new, so I would like to get EF to autogenerate them.
Any ideas?
I have been working with EF5 trying to build an application and have run into a small problem.
I have created a model like
public class TargetBusinessModel
{
public int Id { get; set; }
public Guid BusinessId {get; set; }
public Business Business { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string ContactPhone { get; set; }
}
Updated the Context file
public DbSet<TargetBusinessModel> TargetBusinessModels { get; set; }
My problem is none of the properties from Business are mapped within the database.
The Business Model I am trying to add is from another project, I am not sure if that's the reason.
I don't mind if the code first creates a separate table for my Business model or combines them together.
Can anyone help out?
Try to add DbSet for Business entities to your DbContext implementation:
public DbSet<Business> Businesses { get; set; }
I have a User and an Organization class. They look like this
public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Organization> Organizations { get; set; }
}
public class Organization : EntityBase
{
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<User> Users { get; set; }
}
And both inherit from an EntityBase class to get common fields like Id and created/updated tracking.
public abstract class EntityBase
{
public int Id { get; set; }
public DateTime Created { get; set; }
public virtual User CreatedBy { get; set; }
public DateTime Updated { get; set; }
public virtual User UpdatedBy { get; set; }
}
As denoted by the ICollection properties on both, there should be a many-to-many relation. However when my database is autogenerated I get incorrect foreign keys added to my tables
If I change the CreatedBy and UpdatedBy to be strings instead of User properties I get a join table, which is what I was looking for.
Is this a matter of Entity Framework simply being confused and I need to supply many-to-many configuration in the using fluent mappings, or have I done something wrong?
If you have multiple relationships you need to configure them manually by fluent API or using attributes,
Note:If you have multiple relationships between the same types (for
example, suppose you define the Person and Book classes, where the
Person class contains the ReviewedBooks and AuthoredBooks navigation
properties and the Book class contains the Author and Reviewer
navigation properties) you need to manually configure the
relationships by using Data Annotations or the fluent API
Here is the article from Microsoft.
I have a parent-child relationship setup that is fairly basic. The end result is that I want to be able to return the resulting tables as JSON through ASP.NET MVC WebAPI. I am using Entity Framework 5.0 beta 2.
I can demonstrate the error I'm running into with a simple example. Given the classes Category and Product with the corresponding data context:
public class Category
{
public int CategoryId { get; set; }
public string Title { get; set; }
public virtual IEnumerable<Product> Products { get; set; }
}
public class Product
{
public int ProductId { get; set; }
public string Title { get; set; }
public virtual Category Category { get; set; }
public virtual int CategoryId { get; set; }
}
public class ProductDataContext : DbContext
{
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
}
When I try to run a query that includes the products I get the following error:
A specified Include path is not valid. The EntityType 'FooAndBar.Category'
does not declare a navigation property with the name 'Products'.
The statement to fetch is pretty straightforward:
var everything = dc.Categories
.Include(c => c.Products);
What is the correct way to setup the columns and/or the query so that the Products are included with the Categories?
Child collection properties must be declared as anICollection<T>, not anIEnumerable<T>.
Also, you do not need to explicitly add a CategoryId field to the child class; EF will create that automatically in the database.