Using Entity Framework 5.0 map relationship between parent and children when ID are Guids with newsequentialid() - entity-framework-5

I have two classes with a one to many relationship.
public partial class Client
{
public Client()
{
this.Client_Local = new HashSet<Client_Local>();
}
public System.Guid Guid { get; set; }
public string Name { get; set; }
public virtual ICollection<Client_Local> Client_Local { get; set; }
}
public partial class Client_Local
{
public System.Guid LocalGuid { get; set; }
public System.Guid Guid { get; set; }
public string CultureID { get; set; }
public string LocalName { get; set; }
public virtual Client Client { get; set; }
}
The mapping classes are:
class ClientMapping : EntityTypeConfiguration<Client>
{
public ClientMapping()
{
this.HasKey(entity => entity.Guid); //As newsequentialid()
this.ToTable("Client");
this.Property(entity => entity.Guid).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}
class Client_LocalMapping : EntityTypeConfiguration<Client_Local>
{
public Client_LocalMapping()
{
this.HasKey(entity => entity.LocalGuid); // as As newsequentialid()
this.ToTable("Client_Local");
this.Property(entity => entity.LocalGuid).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.HasRequired<Client>(e => e.Client).WithMany(p => p.Client_Locals).HasForeignKey<Guid>(c => c.Guid);
}
}
I can create new instances of client class and save them to the database successfully. But when I try and add a Client_Local class and save it to the database I get "A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'Guid'.".
The key fields are generated in sql as newsequentialid for performance reasons. And there is a foreign key relationship between Client_Local.Guid and Client.Guid.
Retrieving data from the database works with the above mapping classes. Any help would be greatly appreciated.

Related

The property 'Country.IdCountry' is of type 'Guid' which is not supported by the current database provider

I'm trying to use EF Core in combination with Azure CosmosDB. I'm using the following configuration:
Entities:
public class Country
{
public Guid IdCountry { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public string PhoneCode { get; set; }
public IdNameReference Currency { get; set; }
}
public class IdNameReference
{
public Guid Id { get; set; }
public string Name { get; set; }
}
EntityConfiguration class:
public class CountryEntityConfiguration : IEntityTypeConfiguration<Country>
{
public void Configure(EntityTypeBuilder<Country> builder)
{
builder.ToContainer("Country");
builder.HasKey(e => e.IdCountry);
builder.HasPartitionKey(e => e.IdCountry);
builder.HasNoDiscriminator();
builder.Property(e => e.IdCountry).HasConversion<GuidToStringConverter>().ValueGeneratedOnAdd().HasValueGenerator<GuidValueGenerator>();
builder.HasOne(e => e.Currency).WithMany().Metadata.DependentToPrincipal.SetPropertyAccessMode(PropertyAccessMode.Field);
}
}
DbContext OnModelCreating:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ApplyConfiguration(new CurrencyEntityConfiguration());
}
And the IServiceCollection configuration:
services.AddDbContext<MyDbContext>(options => options.UseCosmos(
Environment.GetEnvironmentVariable("Db_ServiceEndpoint"),
Environment.GetEnvironmentVariable("Db_AccountKey"),
Environment.GetEnvironmentVariable("Db_DatabaseName")
)
);
But I'm still getting the following error:
The property 'Country.IdCountry' is of type 'Guid' which is not supported by the current database provider. Either change the property CLR type, or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'
EDIT: I forgot to specify. This happens when I try to add a new item to the context collection
I'm new in CosmosDB and also in configuration of EF this way.
Do you have an idea, where the problem could be?
Okay, I figured it out, I used wrong the ValueConverter.
I used
.HasConversion<GuidToStringConversion>()
but I had to use
.HasConversion<string>()
or
.HasConversion(g => g.ToString("D"), s => new Guid(s))
I hope it will someone :)

How to fix 'model code generator' in asp.net mvc 5

I modelling a simple library management system that able to register user and the user can issue a book.
I want the asp.net IdentityUser to create one -to many relationShip with BookIssue Custom table. because I am new to asp.net mvc 5, I can not fix the problem please help me.
public class ApplicationUser : IdentityUser
{
public virtual ICollection<BookIssue> BookIssues { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public DbSet<Book> Books { get; set; }
public DbSet<BookIssue> BookIssues { get; set; }
public DbSet<Catagory> Catagories { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
modelBuilder.Entity<BookIssue>()
.HasRequired(n => n.ApplicationUser)
.WithMany(a => a.BookIssues)
.HasForeignKey(n => n.ApplicationUserId)
.WillCascadeOnDelete(false);
}
}
}
BookIssue Model:
public int BookIssueId { get; set; }
public int BookId { get; set; }
public int ApplicationUserId { get; set; }
public string Email { get; set; }
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
public virtual Book Book { get; set; }
public virtual ApplicationUser ApplicationUser { get; set;
The generated error:
There was an error running the selected code generator. 'Unable to
retrieve metadata for Librart.Models.BookIssuee'.One or more
validation errors were detected during model generation:
Library.Models.IdentityUserLogin: EntityType 'IdentityUserRole' has no
key defined.Define the key for this EntityType.
Library.Models.IdentityUserRole: EntityType 'IdentityUserLogin' has no
key defined.Define the key for this EntityType.
And many other errors are generated.

Automapper Object with inside list of object to primitive mapping

I'm trying to create map for automapper to let me map those entity
Entities
public class Entity
{
...
public List<NavigationEntity> Navs { get; set; }
}
public class NavigationEntity
{
public int Id { get; set; }
}
DTO that need to be create with entities
public class EntityDto
{
...
public List<int> NavIds { get; set; }
}
This doesnt seem's to do the job! What could do the job ?
CreateMap<Entity, EntityDto>().ReverseMap();
CreateMap<NavigationEntity, int>().ConstructUsing(x => x.Id);
EDIT
Tried to add
CreateMap< List < SystemsTags >, List< int >>();
but still it doesnt map
First of all, you should rename public List<NavigationEntity> Navs { get; set; } and public List<int> NavIds { get; set; } to the same name. If it is still not working try to also change ConstructUsing to ConvertUsing. And if you need the reverseMap of Entity to EntityDTO you should also add
CreateMap<int, NavigationEntity>().ConvertUsing(x => new NavigationEntity { Id = x });
final code
public class Entity
{
...
public List<NavigationEntity> Navs { get; set; }
}
public class NavigationEntity
{
public int Id { get; set; }
}
public class EntityDto
{
...
public List<int> Navs { get; set; }
}
...
CreateMap<Entity, EntityDto>().ReverseMap();
CreateMap<NavigationEntity, int>().ConvertUsing(x => x.Id);
CreateMap<int, NavigationEntity>().ConvertUsing(x => new NavigationEntity { Id = x });

how do i use AutoMapper in ICollation<> Fields

when i use AutoMapper for mapping my ViewModels and get All News, thrown error for me.
Errors...
The following property on Mosque.Core.ViewModels.CategoryViewModel cannot be mapped:
Categories
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type Mosque.Core.ViewModels.CategoryViewModel.
please help me, thank you
//Models
public class News
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual User User { get; set; }
}
public class Category
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<News> News { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<News> News { get; set; }
}
//ViewModels
public class NewsViewModel
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<CategoryViewModel> Categories { get; set; }
public virtual UserViewModel User { get; set; }
}
public class CategoryViewModel
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<NewsViewModel> News { get; set; }
}
public class UserViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<NewsViewModel> News { get; set; }
}
how do i use for select All News?
--Update1--
I used onion architecture in the project and i installed AutoMapper in the Service layer and i want get all news from repository and fill into ViewModels and pass to the UI.
my code in service layer is...
public List<NewsViewModel> GetAll()
{
Mapper.CreateMap<News, NewsViewModel>()
.ForMember(dest => dest.Categories, src => src.MapFrom(p => p.Categories))
.ForMember(dest => dest.User, src => src.MapFrom(p => p.User));
Mapper.AssertConfigurationIsValid();
var viewModels = new List<NewsViewModel>();
foreach (var item in _newsRepository.GetAll())
{
var viewModel = Mapper.Map<News, NewsViewModel>(item);
viewModels.Add(viewModel);
}
return viewModels;
}
You don't seem to have created maps for Catagory and User.
Add the following maps:
Mapper.CreateMap<User, UserViewModel>();
Mapper.CreateMap<Category, CategoryViewModel>();
By the way, why are you creating the maps inside the GetAll method? You can create the maps once, usually at application startup.

Entity Framework 4.0 CTP5 Many to Many relation ship with a help table?

i have a question about EF4 again :) sorry i can't find any thing on the net.
The question is i am using CTP5 Code Only to map (many to many), how do i do it???
But i am doing the old fashion way, i mean i have three tables:
Users
Companies.
UsersToCompanies -> (UserId, CompanyId)
How do i map this, it will be really appreciated if you would show me the code example to it,
from POCO's to Mapping.
This is the error i receive...
This is my entities
public class User
{
public int UserId { get; set; }
public int? UserRoleId { get; set; }
public string UserName { get; set; }
public string UserPassword { get; set; }
public DateTime InsertDate { get; set; }
public virtual UserRole UserRole { get; set; }
//this is my many to many prop
public virtual ICollection<Company> Companies { get; set; }
}
public class Company
{
public int CompanyId { get; set; }
public string CompanyNameHe { get; set; }
public string CompanyNameEn { get; set; }
public string CompanyParent { get; set; }
public DateTime InsertDate { get; set; }
//this is my many to many prop
public virtual ICollection<User> Users { get; set; }
}
/*relationship map*/
public class UsersCompanies
{
public int Id { get; set; }
public int UserId { get; set; }
public int CompanyId { get; set; }
}
//mapping
public class CompanyMap : BaseConfig<Company>
{
public CompanyMap()
{
/*Identity*/
HasKey(c => c.CompanyId);
Property(c => c.CompanyId).HasDatabaseGenerationOption(DatabaseGenerationOption.Identity).HasColumnName("COMPANY_ID");
/*Have default values*/
Property(c => c.InsertDate).HasDatabaseGenerationOption(DatabaseGenerationOption.Computed).HasColumnName("INSERT_DATE");
/*simple scalars*/
Property(c => c.CompanyNameHe).HasMaxLength(32).IsRequired().HasColumnName("COMPANY_NAME_HE");
Property(c => c.CompanyNameEn).HasMaxLength(32).IsRequired().HasColumnName("COMPANY_NAME_EN");
Property(c => c.CompanyParent).HasMaxLength(32).IsRequired().HasColumnName("COMPANY_PARENT");
ToTable("CMS_COMPANY", "GMATEST");
}
}
public class UserMap : BaseConfig<User>
{
public UserMap()
{
/*Identity*/
HasKey(c => c.UserId);
Property(c => c.UserId).HasDatabaseGenerationOption(DatabaseGenerationOption.Identity).HasColumnName("USER_ID");
/*Have default values*/
Property(c => c.InsertDate).HasDatabaseGenerationOption(DatabaseGenerationOption.Computed).HasColumnName("INSERT_DATE");
/*simple scalars*/
Property(c => c.UserName).HasMaxLength(25).IsRequired().HasColumnName("USER_NAME");
Property(c => c.UserPassword).HasMaxLength(25).IsRequired().HasColumnName("USER_PASSWORD");
Property(c => c.UserRoleId).IsRequired().HasColumnName("USER_ROLE_ID");
/*relationship*/
HasRequired(u => u.UserRole).WithMany().HasForeignKey(t => t.UserRoleId);
HasMany(p => p.Companies).WithMany(c => c.Users).Map(mc =>
{
mc.ToTable("UsersCompanies");
mc.MapLeftKey(p => p.UserId, "CompanyId");
mc.MapRightKey(c => c.CompanyId, "UserId");
});
ToTable("CMS_USERS", "GMATEST");
}
}
public class UsersCompaniesMap : BaseConfig<UsersCompanies>
{
public UsersCompaniesMap()
{
/*Identity*/
HasKey(k => k.Id);
Property(c => c.Id).HasDatabaseGenerationOption(DatabaseGenerationOption.Identity).HasColumnName("ID");
Property(c => c.UserId).IsRequired().HasColumnName("USER_ID");
Property(c => c.CompanyId).IsRequired().HasColumnName("COMPANY_ID");
ToTable("CMS_USERS_TO_COMPANIES", "GMATEST");
}
}
Here's the error I get:
'((new
System.Linq.SystemCore_EnumerableDebugView(ctx.FindAll())).Items[0]).Companies'
threw an exception of type
'System.Data.EntityCommandExecutionException'
Inner exception: ORA-00942: table or
view does not exist
You do not really need 3 tables to perform the mapping. You could simply do the following:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Company> Companies { get; set; }
}
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<User> Users { get; set; }
}
And this should do it. As long as you have a virtual ICollection referencing the other entity in both POCO's, CTP5's conventions should take care of everything for you. On the other hand, if you prefer doing it manually using the Fluent API, then check this blog by the ADO.NET team (scroll a little down to the many-to-many relationship section)
The answer for my problem, i have to say many thanks to you all, but Andrey from Devart,
have given me the solution, it's simple.
First off all i have to have, three tables Companies, Users, UsersCompanies/
Second off all i have to map the tables to original names of tables example:
my users table called in Db like that: CMS_USERS i have to map it with original name.
this is the example that i use and it's really works.
HasMany(c => c.Companies)
.WithMany(u => u.Users)
.Map(mc =>
{
mc.ToTable("CMS_USERS_TO_COMPANIES", "GMATEST");
mc.MapLeftKey(c => c.UserId, "USER_ID");
mc.MapRightKey(u => u.CompanyId, "COMPANY_ID");
});
Thank you all.

Resources