invalid object name 'dbo.EvaluationTerms' - asp.net-mvc

I have two project folders- Proj.Domain.Core.ClassAgg and Proj.Domain.Evaluation.EvaluationAgg which contain my models classes.
My Year class:
namespace Proj.Domain.Core.ClassAgg
{
public class Year
{
public Year()
{
EvaluationTerms = new List<EvaluationTerm>();
}
public int Id {get;set;}
public int ClassGroupId { get; set; }
public int AcademicYearId { get; set; }
public int NumDivisions { get; set; }
public int PlannedStrength { get; set; }
public virtual ICollection<EvaluationTerm> EvaluationTerms { get; private set; }
}
}
My EvaluationTerm class in different project:
namespace Proj.Domain.Evaluation.EvaluationAgg
{
public class EvaluationTerm
{
public int Id {get;set;}
public int YearId { get; set; }
public string Description { get; set; }
}
}
Proj.Domain.Core.ClassAgg has reference to Proj.Domain.Evaluation.EvaluationAgg.
I am trying to access ClassYear collection from service class in Proj.Domain.Evaluation.EvaluationAgg.
namespace Proj.Application.Core.ClassSvc
{
public class YearService : IYearService
{
private readonly IYearRepository _yearRepository;
public YearService(IClassYearRepository yearRepository)
{
_yearRepository = yearRepository;
}
public List<Year> GetYears()
{
return _yearRepository.GetAll();
}
}
}
Even if I reference Year namespace in Application project, I am not able to retrieve EvaluationTerm data as it is in different project.
Error:
In collection.
invalid object name 'dbo.EvaluationTerms'.

Related

Table Value Function mapping with Entity Framework 6.3

How do I map table value functions in Code with Entity Framework 6.3? I'm trying to a DB Context in code with an existing database, because EDMX is currently not supported in ASP.NET Core 3. I've tried setting up my DbContext clasee as below. I can successfully query the Grade table. But when I try to query my function "fn_GetCatgeories", I get the following error: No EdmType found for type 'WebApplication6.Data.ApplicationContext+fn_GetCategories'.
public class ApplicationContext : DbContext
{
public ApplicationContext(string cstr)
: base(cstr)
{
Database.SetInitializer<ApplicationContext>(null);
}
[Table("Grade")]
public class Grade
{
[Key]
public string Grade_ID { get; set; }
public string SchoolType { get; set; }
public int Sortorder { get; set; }
}
public partial class fn_GetCategories
{
public int Category_ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Nullable<bool> Active { get; set; }
public Nullable<System.DateTime> Month { get; set; }
public Nullable<int> Order { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add(new FunctionsConvention<ApplicationContext>("dbo"));
base.OnModelCreating(modelBuilder);
}
[DbFunction("ApplicationContext", "fn_GetCategories")]
public IQueryable<fn_GetCategories> GetCategories(Nullable<System.DateTime> month)
{
var monthParameter = month.HasValue ?
new ObjectParameter("Month", month) :
new ObjectParameter("Month", typeof(System.DateTime));
return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<fn_GetCategories>(string.Format("[0].{1}", GetType().Name, "[fn_GetCategories](#Month)"), monthParameter);
}
// DbSets here
public DbSet<Grade> Grades { get; set; }
}
This works:
public class ApplicationContext : DbContext
{
public ApplicationContext(string cstr)
: base(cstr)
{
Database.SetInitializer<ApplicationContext>(null);
}
[Table("Grade")]
public class Grade
{
[Key]
public string Grade_ID { get; set; }
public string SchoolType { get; set; }
public int Sortorder { get; set; }
}
public partial class fn_GetCategories_Result
{
[Key]
public int Category_ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Nullable<bool> Active { get; set; }
public Nullable<System.DateTime> Month { get; set; }
public Nullable<int> Order { get; set; }
}
// DbSets here
public DbSet<Grade> Grades { get; set; }
public DbSet<fn_GetCategories_Result> fn_GetCategoriesSet { get; set; }
public List<fn_GetCategories_Result> fn_GetCategories(DateTime month)
{
var m = new SqlParameter("Month", DateTime.Now);
return this.fn_GetCategoriesSet.SqlQuery("select * from dbo.fn_GetCategories(#month)", m).ToList();
}

posting customer details with 'n ' no of address

I have two model classes "customerdetails" and "addresssdetails".my question is "one customer can save with multiple addresss?
namespace customer2.Models
{
public class customerdetails
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { set; get; }
[Key]
public int customerid { set; get; }
public string customername { set; get; }
}
public class addressdetails
{
public int addressno { set; get; }
public string street { set; get; }
public string landmark { set; get; }
public int pincode { set; get; }
}
public class MkContext : DbContext
{
public virtual DbSet<customerdetails> customers { get; set; }
public virtual DbSet<addressdetails> address { get; set; }
}
public class customerviewmodel
{
public customerdetails cd { set; get; }
public List<addressdetails> ad { set; get; }
}
}![enter image description here](https://i.stack.imgur.com/4QCA1.jpg)
Before I answer, just a quick tip:
Make sure to properly case your class names and properties to improve readability.
Here is a link with some guidelines
If you want to store a Customer with multiple AddressDetails, you will need to define a class and add it to the context.
public class Customer {
public int Id { get; set; }
public CustomerDetails CustomerDetails { get; set; }
public IList<AddressDetails> AddressDetails { get; set; }
}
Then add this to the context.

Automapper Mapping for Localization Resolver in a Multi-Language Website

I found a way to use Automapper for Language mapping based on the active Culture.
The question is if it's possible to build a generic Resolver to map all the models that use the Resolver.
In this Case, the models to map have always the same properties, Id and Name (including the language properties Name_PT, Name_FR and Name_EN):
// MODELS
public class MakeDto
{
// Primary properties
public int Id { get; set; }
public string Name { get; set; }
public string Name_PT { get; set; }
public string Name_FR { get; set; }
public string Name_EN { get; set; }
}
public class MakeViewModel
{
// Primary properties
public int Id { get; set; }
public string Name { get; set; }
}
public class ModelDto
{
// Primary properties
public int Id { get; set; }
public string Name { get; set; }
public string Name_PT { get; set; }
public string Name_FR { get; set; }
public string Name_EN { get; set; }
}
public class ModelViewModel
{
// Primary properties
public int Id { get; set; }
public string Name { get; set; }
}
public class FuelDto
{
// Primary properties
public int Id { get; set; }
public string Name { get; set; }
public string Name_PT { get; set; }
public string Name_FR { get; set; }
public string Name_EN { get; set; }
}
public class FuelViewModel
{
// Primary properties
public int Id { get; set; }
public string Name { get; set; }
}
// AUTOMAPPER PROFILE
public class DtoToViewModelMappingProfile : Profile
{
public override string ProfileName
{
get { return "DtoToViewModelMappings"; }
}
protected override void Configure()
{
CreateMaps();
}
private static void CreateMaps()
{
Mapper.CreateMap<ModelDto, ModelViewModel>();
Mapper.CreateMap<MakeDto, MakeViewModel>()
.ForMember(dest => dest.Name, opt => opt.ResolveUsing<CultureResolver>());
Mapper.CreateMap<FuelDto, FuelViewModel>();
}
public class CultureResolver : ValueResolver<MakeDto, string>
{
protected override string ResolveCore(MakeDto makeDto)
{
switch(Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToUpperInvariant())
{
case "FR":
return makeDto.Name_FR;
case "EN":
return makeDto.Name_EN;
}
return makeDto.Name_PT;
}
}
}
Thanks.
You can extract an interface like below:
public interface ILocalizable
{
string Name { get; set; }
string Name_PT { get; set; }
string Name_FR { get; set; }
string Name_EN { get; set; }
}
public class FuelDto : ILocalizable
{
// Primary properties
public int Id { get; set; }
public string Name { get; set; }
public string Name_PT { get; set; }
public string Name_FR { get; set; }
public string Name_EN { get; set; }
}
Then tune your resolver like below:
public class CultureResolver : ValueResolver<ILocalizable, string>
{
protected override string ResolveCore(ILocalizable dto)
{
switch(Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToUpperInvariant())
{
case "FR":
return dto.Name_FR;
case "EN":
return dto.Name_EN;
}
return dto.Name_PT;
}
}

Model or ViewModel in a ListViewModel

Should T be a for example Customer or CustomerViewModel ?
The annotations bound to Mvc namespace are on the ListViewModel so actually I could pass the Customer object. What do you think?
public class ListViewModel<T>
{
[Required(ErrorMessage="No item selected.")]
public int[] SelectedIds { get; set; }
public IEnumerable<T> DisplayList { get; set; }
}
UPDATE
[HttpGet]
public ActionResult Open()
{
IEnumerable<Testplan> testplans = _testplanDataProvider.GetTestplans();
OpenTestplanListViewModel viewModel = new OpenTestplanListViewModel(testplans);
return PartialView(viewModel);
}
public class OpenTestplanListViewModel
{
public OpenTestplanListViewModel(IEnumerable<Testplan> testplans)
{
var testplanViewModels = testplans.Select(t => new TestplanViewModel
{
Name = string.Format("{0}-{1}-{2}-{3}", t.Release.Name, t.Template.Name, t.CreatedAt, t.CreatedBy),
TestplanId = t.TestplanId,
});
DisplayList = testplanViewModels;
}
[Required(ErrorMessage = "No item selected.")]
public int[] SelectedIds { get; set; }
public string Name { get; set; }
public IEnumerable<TestplanViewModel> DisplayList { get; private set; }
}
public class TestplanViewModel
{
public int TestplanId { get; set; }
public string Name { get; set; }
}
public class Testplan
{
public int TestplanId { get; set; }
public int TemplateId { get; set; }
public int ReleaseId { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedAt { get; set; }
public Template Template { get; set; }
public Release Release { get; set; }
}
T should ideally be a view model. Having a view model referencing domain models is some kind of a hybrid view model, not a real one. But if you think that in this specific case the domain model will be exactly the same as the view model then you could keep it as well.

Define the key for this EntityType

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

Resources