I have a viewmodel as such:
namespace Lipton.Areas.Drugs.Models
{
public class DrugViewModel
{
public string ID { get; set; }
public IEnumerable<tblDrug> DrugList { get; set; }
}
}
The above works fine. The reason why it works is because for tblDrug is in the appropriate namespace:
Lipton.Areas.Drugs.Models. What happens though if I need to add an IEnumerable for another table - tblEmp which is in a totally different namespace (Lipton.Areas.Empl.Models:
namespace Lipton.Areas.Drugs.Models
{
public class DrugViewModel
{
public string ID { get; set; }
public IEnumerable<tblDrug> DrugList { get; set; }
public IEnumerable<tblEmp> EmpList { get; set; }
}
}
How would I modify the above code to work due to the namespace issue?
You add a using directive in order to bring the namespace into scope so that you could directly use the types declared in this namespace without fully qualifying them:
namespace Lipton.Areas.Drugs.Models
{
using Lipton.Areas.Empl.Models
public class DrugViewModel
{
public string ID { get; set; }
public IEnumerable<tblDrug> DrugList { get; set; }
public IEnumerable<tblEmp> EmpList { get; set; }
}
}
I don't even know why this question is tagged with asp.net-mvc. That's basic c#.
You could explicitly add the namespace for tblEmp
namespace Lipton.Areas.Drugs.Models
{
public class DrugViewModel
{
public string ID { get; set; }
public IEnumerable<tblDrug> DrugList { get; set; }
public IEnumerable<Lipton.Areas.Empl.Models.tblEmp> EmpList { get; set; }
}
}
Related
I have relationship one to one
public class Book
{
public int BookId { get; set; }
public string Name { get; set; }
public string Annotation { get; set; }
public virtual File File { get; set; }
public int? SeriesId { get; set; }
public DateTime UploadDate { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual ICollection<Author> Authors { get; set; }
public virtual ICollection<Genre> Genres { get; set; }
public virtual ICollection<Mark> Marks { get; set; }
public Book()
{
Comments = new List<Comment>();
Authors = new List<Author>();
Genres = new List<Genre>();
}
}
public class File
{
[Key,ForeignKey("Book")]
public int BookId { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }
public byte[] Content { get; set; }
public virtual Book Book { get; set; }
}
And I want to transfer data to classes:
public class BookDO
{
public int BookId { get; set; }
public string Name { get; set; }
public string Annotation { get; set; }
public virtual FileDO File { get; set; }
}
public class FileDO
{
public int BookId { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }
public byte[] Content { get; set; }
public virtual BookDO Book { get; set; }
}
in such way:
var books = Database.Books.GetAll().ToList();
Mapper.Initialize(cf => cf.CreateMap<Book, BookDO>());
return Mapper.Map<List<Book>, List<BookDO>>(books);
but i'm getting Missing type map configuration or unsupported mapping.
Mapping types:
File -> FileDO
Domain.File -> BusinessLogic.Data_Objects.FileDO
Maybe i need to initialize one more mapper to map File to FileDO or modify existing mapper configuration? help me please.
Yes, you also need to create a map for File -> FileDo. This map must be configured for the same mapper as used for Book -> BookDo.
It is good practice to wrap your mapping configuration into an AutoMapper.Profile:
using AutoMapper;
public class BookMappingProfile: Profile {
public BookMappingProfile() {
CreateMap<Book, BookDo>();
CreateMap<File, FileDo>();
}
}
And then initialize the mapper with these profiles:
Mapper.Initialize(cfg => {
cfg.AddProfile<BookMappingProfile>();
cfg.AddProfile<MyOtherProfile>();
});
I am trying to ignore a class property when inserting data to database using metadata for the class but it is not working. I am using using EF 6. I have tried both the metadata and partial class are in the same assembly as the classes generated by EF
[NotMapped] and [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
Used a internal sealed class (for metadata) inside my partial class
namespace XeroDataStore.XeroDatabase
{
[MetadataType(typeof(TempAddressMetadata))]
public partial class TempAddress
{
}
[MetadataType(typeof(TempContact.TempContactMetadata))]
public partial class TempContact
{
internal sealed class TempContactMetadata
{
[NotMapped]
public Nullable<System.DateTime> UploadDate { get; set; }
}
}
}
namespace XeroDataStore.XeroDatabase
{
public class TempAddressMetadata
{
[NotMapped]
public Nullable<System.DateTime> UploadDate { get; set; }
}
}
EF Generated Class
namespace XeroDataStore.XeroDatabase
{
public partial class TempAddress
{
public int RowId { get; set; }
public int ClientID { get; set; }
public System.Guid ContactID { get; set; }
public string AddressType { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
public string AddressLine4 { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string AttentionTo { get; set; }
public Nullable<System.DateTime> UploadDate { get; set; }
public virtual TempContact TempContact { get; set; }
}
}
What am I missing here?
Do it using Fluent API to make sure your model classes are POCO and have nothing to do with the data access.
In your data context, OnModelCreating methoed, use the following code to ignore the property
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TempContact>().Ignore(a => a.UploadDate );
}
As far as i know, i have two way to implement many-to-many relation in asp.net mvc using code-first.
1- Fluent Api
public class HrPerson
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<HrPersonTitle> HrPersonTitle { get; set; }
}
public class HrPersonTitle
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<HrPerson> HrPerson { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<HrPerson>()
.HasMany(s => s.HrPersonTitle)
.WithMany(c => c.HrPerson)
.Map(t =>
{
t.MapLeftKey("HrPersonId")
.MapRightKey("HrPersonTitleId")
.ToTable("HrMapPersonTitle");
});
}
2-Custom Mapping Table
public class HrPerson
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<HrMapPersonTitle> HrMapPersonTitle { get; set; }
}
public class HrPersonTitle
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<HrMapPersonTitle> HrMapPersonTitle { get; set; }
}
public class HrMapPersonTitle
{
public int Id { get; set; }
public int HrPersonId { get; set; }
public int HrPersonTitleId { get; set; }
public virtual HrPerson HrPerson { get; set; }
public virtual HrPersonTitle HrPersonTitle { get; set; }
public string Note { get; set; }
public bool Deleted { get; set; }
}
My questions:
If i choose second way, i am not able to reach HrPersonTitle.Name property from HrPerson model in the view. How can i reach the properties ?
If i choose the first way i can reach the HrPersonTitle.Name but i am not able to add more property in the map file ? How can i add more properties?
Regards.
When you create a M2M without a payload (just the foreign key relationships, no extra data), EF collapses the relationship so that you can query directly without having to explicitly go through the join table. However, if you need a payload, then EF can no longer manage the relationship in this way.
So, if you want to get the title, you have to go through HrMapPersonTitle:
#foreach (var title in Model.HrMapPersonTitle)
{
#title.HrPersonTitle.Name
}
Both these methods seem overkill maybe. I don't know your full intentions however I implement this all the time at work and I use the following:
public class HrPerson
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<HrPersonTitle> HrPersonTitles { get; set; }
}
public class HrPersonTitle
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<HrPerson> HrPersons { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<HrPerson>()
.HasMany(s => s.HrPersonTitles)
.WithMany(c => c.HrPersons);
}
If you are using code first and you try and access either mapping within the DbContext it should Lazy Load your information and every property should be accessible.
I do have one question though. Are you sure it should be many to many, do they really have multiple titles?
I'm using Entity Framework and MVC3, and my problem is that I can't scaffold Controllers if the class inherits from another Class.
Example:
This is Base Class
using System;
using System.Collections.Generic;
namespace CRMEntities
{
public partial class Company
{
public int Id { get; set; }
}
}
This is Lead Class (Child)
using System;
using System.Collections.Generic;
namespace CRMEntities
{
public partial class Lead : Company
{
public Lead()
{
this.Status = 1;
this.IsQualified = false;
}
public Nullable<short> Status { get; set; }
public Nullable<bool> IsQualified { get; set; }
}
}
When I tried to add controller below error comes...
Context Class COde
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
namespace CRMEntities
{
public partial class CRMWebContainer : DbContext
{
public CRMWebContainer()
: base("name=CRMWebContainer")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Employee> Employees { get; set; }
public DbSet<Contact> Contacts { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Location> Locations { get; set; }
public DbSet<Task> Tasks { get; set; }
public DbSet<EventInfo> EventInfoes { get; set; }
public DbSet<Opportunity> Opportunities { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<Document> Documents { get; set; }
public DbSet<LoginInformation> LoginInformations { get; set; }
public DbSet<CRMLog> CRMLogs { get; set; }
public DbSet<EntitySharing> EntitySharings { get; set; }
public DbSet<EntityFlagging> EntityFlaggings { get; set; }
public DbSet<EntityTagging> EntityTaggings { get; set; }
public DbSet<EntitySubscribing> EntitySubscribings { get; set; }
public DbSet<Compapny> Compapnies { get; set; }
}
}
The MVC AddController window check for a property DbSet of the ModelType you are adding.
You should do like vicentedealencar said, add to your CRMWebContainer:
[Obsolete("Design only", true)]
public DbSet<Lead> Leads { get; set; }
Remember that u should not use this property in your code (this is why the Obsolete Attribute), since the right way to get the Leads is using:
var leads = Companies.OfType< Lead >();
I get this error on this line of code -
ReportRunnerEntities reportDB = new ReportRunnerEntities();
public ActionResult Index()
{
**var types = reportDB.ReportTypes.ToList();**
return View(types);
}
The tables in the databse have primary keys defined and identities set.
My models are -
namespace ReportRunner.Models
{
public partial class ReportRunnerEntities : DbContext
{
public DbSet<Reports> Report { get; set; }
public DbSet<ReportTypes> ReportTypes { get; set; }
public DbSet<Users> Users { get; set; }
}
}
namespace ReportRunner.Models
{
public partial class ReportTypes
{
public int ReportTypeId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Reports> Reports { get; set; }
}
}
namespace ReportRunner.Models
{
public class Reports
{
public int ReportId { get; set; }
public int ReportTypeId { get; set; }
public int UserId { get; set; }
public string Title { get; set; }
public ReportTypes ReportType { get; set; }
}
}
namespace ReportRunner.Models
{
public partial class Users
{
public int UserId { get; set; } //ArtistId
public string Name { get; set; }
}
}
and here is my connection string -
I suspect that it's never reaching the database. As I said the keys are set in the database.
Am I missing something?
There are a couple things I see that should change:
ReportTypes should be ReportType
public List Reports { get;
set; } should be public
ICollection Reports { get;
set; }
If you are defining a
connection string in your web.config,
you need to tell EF what one it is
using the constructor in your
ReportRunnerEntities class like this:
namespace ReportRunner.Models
{
public partial class ReportRunnerEntities : DbContext
{
public ReportRunnerEntities : base("name=NameOfConnectionInWebConfig")
{}
public DbSet<Reports> Report { get; set; }
public DbSet<ReportTypes> ReportTypes { get; set; }
public DbSet<Users> Users { get; set; }
}
}
You can read more on that here : http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-2-connections-and-models.aspx
Just on a side note, if you are planning on using .NET MVC and EF Code First as your stack, I would start using the Repository and Unit of Work pattern. Here is a good post on how to set that up: Entity Framework 4 CTP 4 / CTP 5 Generic Repository Pattern and Unit Testable