Cannot find the object "TABLE" because it does not exist or you do not have permissions - asp.net-mvc

I know there are N no of solutions with same title and after 3 days and useless running through them all I have to post it here.
I've an UserDetailDAC.cs like this:-
public UserDetails ValidateUserAdmin(string Usrname, string pwd)
{
using (var db = new HostelManagementContext())
{
//IEnumerable<UserDetails> linq;
//string query = "SELECT* FROM UserDetails WHERE UserName ='" + Usrname + "' COLLATE SQL_Latin1_General_CP1_CS_AS AND UserPwd = '" + pwd + "' COLLATE SQL_Latin1_General_CP1_CS_AS";
//linq = db.Database.SqlQuery<UserDetails>(query);
//return linq.FirstOrDefault();
return db.Set<UserDetails>().Where(x => x.UserName == Usrname && x.UserPwd == pwd).FirstOrDefault();
}
}
and my HostelManagementContext.cs is like this :-
public HostelManagementContext() : base("ConnectionString")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<HostelManagementContext, HostelModel.Migrations.Configuration>("ConnectionString"));
//Database.SetInitializer<HostelManagementContext>(new DropCreateDatabaseIfModelChanges<HostelManagementContext>());
}
public DbSet<CandidateHostelBookingMapping> CandidateHostelBookingMappings { get; set; }
public DbSet<UserDetails> UserDetails { get; set; }
Now when I try to run this it gives me below error:-
Cannot find the object "CandidateHostelBookingMappings" because it does not exist or you do not have permissions.
This is Entity Framework Code first approach. Please feel free to ask any further details

//Are you use Migration for creating database and table using package manager console ?
enable-migrations -ContextTypeName HostelManagementContext
add-migration -ConfigurationTypeName -----
update-database -ProjectName

Related

How to use COLLATE Latin1_General_bin in Entity framework?

I have to use COLLATE in entity framework query. How to write SQL query equivalent in Entity Framework as show below code?
SQL query:
select * from AspNetUsers order by Email COLLATE Latin1_General_bin
Entity Framework:
using (var db = new testEntities())
{
var appUsers = await db.Users.OrderBy(x => x.Email).ToListAsync();
}
It's possible to use Entity Framework's interception hooks.
The first step it to define an interface:
interface ISortInterceptable
{
IEnumerable<string> AdaptableSortFieldNames { get; set; }
}
Then make your context implement it:
class TestEntities : DbContext, ISortInterceptable
{
...
public IEnumerable<string> AdaptableSortFieldNames { get; set; }
...
}
Next, create a command interceptor:
class SortCommandInterceptor : DbCommandInterceptor
{
public override void ReaderExecuting(DbCommand command,
DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
if (interceptionContext.DbContexts.First() is ISortInterceptable interceptable
&& interceptable.AdaptableSortFieldNames != null)
{
var query = command.CommandText;
foreach (var fieldName in interceptable.AdaptableSortFieldNames)
{
var pattern = $#"(.*\s*ORDER BY\s*.*\.)(\[{fieldName}\])(.*)";
query = Regex.Replace(query, pattern, "$1$2 COLLATE Latin1_General_bin $3");
}
command.CommandText = query;
}
base.ReaderExecuting(command, interceptionContext);
}
}
This is where all the magic happens.
The interceptor first checks if it has to do with a ISortInterceptable (maybe this check can be refined by getting all ISortInterceptables from interceptionContext.DbContexts).
The command text in the command to be executed is analyzed on any occurence of strings like ORDER BY [Alias].[fieldName] where fieldName is a variable. This search pattern is in keeping with the pattern EF always follows to generates queries.
The field name part of the ORDER BY clause, which is in the third group ($2) of the regex match, is extended by the collation phrase.
The replacement is repeated for all field names.
Finally, an example of how to use this interceptor:
DbInterception.Add(new SortCommandInterceptor());
using (var db = new TestEntities())
{
db.AdaptableSortFieldNames = new[] { "LastName", "Email" };
var users = db.AspNetUsers
.OrderBy(u => u.LastName)
.ThenBy(u => U.Email)
.ToList();
}
As always with string manipulation, one caveat: this works in a couple of my own tests, but I can't guarantee it to be rock solid. For one, the sorting fields should be text fields, of course.
One last note. EF core 3 also offers interception hooks that can be used in a similar way.

Code First Migration change string into datetime

There was property in my Task model
public string DateTarget { get; set; }
I created database (I use code first approach)
then I need to change this property
like
private DateTime _dateTarget;
public DateTime DateTarget
{
get { return _dateTarget; }
set
{
_dateTarget = value.ToString(CultureInfo.InvariantCulture) != string.Empty ? value : DateTime.MinValue;
}
}
I used this commands in PM Console
Enable-Migrations
Add-Migration UpdateDB
Update-Database
but I got an error
Error Number:241,State:1,Class:16
Conversion failed when converting date and/or time
from character string.
What's wrong? How to fix it?

MVC Entity Framework - Inserting Data - A dependent property in a ReferentialConstraint is mapped to a store-generated column

I need help with trying to insert a record using MVC and Entity Framework. I have a dynamically created form which can contain many questions. When Editing, I want to delete the existing answers (which it does successfully) and insert new answers.
I am getting the following error:
Cannot insert explicit value for identity column in table 'tblModeratorReportAnswers' when IDENTITY_INSERT is set to OFF.
If I add the following line in my DbContext class
modelBuilder.Entity<QuestionAnswer>().Property(p => p.AnswerID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); I get this error:
A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'AnswerID'.
Here's my code that is doing the update
//
// POST: /Home/Edit/1
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(FormCollection formCollection, int moderatorReportId)
{
ModeratorReport reportToEdit = repository.GetModeratorReportById(moderatorReportId);
List<QuestionAnswer> originalReportAnswers = repository.GetAllModeratorReportAnswers(moderatorReportId).ToList();
foreach (QuestionAnswer answer in originalReportAnswers) {
repository.DeleteAnswer(answer);
}
repository.Save();
int sectionID;
int questionID;
foreach (string key in formCollection.AllKeys)
{
var value = formCollection[key.ToString()];
Match m = Regex.Match(key, "section(\\d+)_question(\\d+)");
if (m.Success) {
QuestionAnswer newAnswer = new QuestionAnswer();
sectionID = Convert.ToInt16(m.Groups[1].Value.ToString());
questionID = Convert.ToInt16(m.Groups[2].Value.ToString());
newAnswer.ModeratorReportID = moderatorReportId;
newAnswer.QuestionID = questionID;
newAnswer.Answer = value;
repository.AddAnswer(newAnswer);
}
}
repository.Save();
reportToEdit.Status = "SUBJECTOFFICER SAVED";
AuditItem auditItem = new AuditItem();
auditItem.ModeratorReportID = moderatorReportId;
auditItem.Status = "SUBJECTOFFICER SAVED";
auditItem.AuditDate = DateTime.Now;
auditItem.Description = "The Moderator report ID: " + moderatorReportId + " was saved.";
auditItem.UserID = User.Identity.Name;
db.Audit.Add(auditItem);
repository.Save();
return RedirectToAction("Details", new { id = moderatorReportId });
}
...and in my repository
//
// Persistance
public void Save()
{
db.SaveChanges();
}
public void AddAnswer(QuestionAnswer answer)
{
db.Answers.Add(answer);
Save();
}
public void DeleteAnswer(QuestionAnswer answer)
{
db.Answers.Attach(answer);
db.Answers.Remove(answer);
}
I have also checked all my Primary Keys, Foreign Keys and they are all ok. The Primary Keys are all set to 'Is Identity'.
I've been trying to sort this problem out all day. I have no idea what to do to resolve it. If anyone can give my any advice, it'd be much appreciated.
Maybe it's my inexperience with ASP.NET MVC and Entity Framework, but I have now resolved this issue by changing the logic of that I update the report.
Instead of deleting the answers and reinserting them. I now retrieve the answers and change Answer property to be the new answer. Then just use db.SaveChanges().
//
// POST: /Home/Edit/1
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(FormCollection formCollection, int moderatorReportId)
{
ModeratorReport reportToEdit = repository.GetModeratorReportById(moderatorReportId);
List<QuestionAnswer> originalReportAnswers = repository.GetAllModeratorReportAnswers(moderatorReportId).ToList();
int sectionID;
int questionID;
foreach (string key in formCollection.AllKeys)
{
var value = formCollection[key.ToString()];
Match m = Regex.Match(key, "section(\\d+)_question(\\d+)");
if (m.Success) {
QuestionAnswer newAnswer = new QuestionAnswer();
sectionID = Convert.ToInt16(m.Groups[1].Value.ToString());
questionID = Convert.ToInt16(m.Groups[2].Value.ToString());
foreach(QuestionAnswer answerToEdit in originalReportAnswers) {
if (answerToEdit.QuestionID == questionID)
{
answerToEdit.Answer = value;
}
}
}
}
repository.Save();
reportToEdit.Status = "SAVED";
AuditItem auditItem = new AuditItem();
auditItem.ModeratorReportID = moderatorReportId;
auditItem.Status = "SAVED";
auditItem.AuditDate = DateTime.Now;
auditItem.Description = "The Moderator report ID was saved.";
auditItem.UserID = User.Identity.Name;
db.Audit.Add(auditItem);
repository.Save();
return RedirectToAction("Details", new { id = moderatorReportId });
}
Cannot insert explicit value for identity column in table
'tblModeratorReportAnswers' when IDENTITY_INSERT is set to OFF.
This error says that you are explicitly inserting value into autogenerated column (identity column).
A dependent property in a ReferentialConstraint is mapped to a
store-generated column. Column: 'AnswerID'.
This error says that there is some incorrectly configured relation where autogenerated AnswerID is considered as FK - that is not supported. Identity and Computed properties must not be FKs.

MVC 3 Reusable Remote Validation with DB Lookup

I have an MVC 3 appliation which I have many integer fields on a form. They all require range validation but the ranges exists in a table in my database. So I would like to create a reusable remote validation tool which will look up the min and max value and return the validation to the view.
I am updating this with some example code of what I would like to do to see my request might clarify what I am looking for:
In my validation class:
[Remote("CheckIntegerRange", "Validation", ErrorMessage = "Value outside of range")]
public object UW1_Web_Tension_SP { get; set; }
[Remote("CheckIntegerRange", "Validation", ErrorMessage = "Value outside of range")]
public object UW2_Web_Tension_SP { get; set; }
[Remote("CheckIntegerRange", "Validation", ErrorMessage = "Value outside of range")]
public object UW3_Web_Tension_SP { get; set; }
In my ValidationController I tried to create a function with multiple parameters but I dont think I can - however I think it shows what I am trying to do more clearly:
public class ValidationController : Controller
{
public JsonResult CheckIntegerRange(int integer, string EntityName, string AttributeName)
{
var result = false;
int MinInteger = 0;
int MaxInteger = 100;
//declare recipe entities
var context = new MadicoRecipeEntities();
//set sql statements and get description, etc from attributes view
var esqlIntegerAttributeDetails = "SELECT VALUE c FROM MadicoRecipeEntities.v_AttributeIntegerRangeDetails AS c " +
"WHERE c.EntityName = '" + EntityName + "' AND c.Attribute = '" + AttributeName + "'";
var queryAttributeDetails = context.CreateQuery<v_AttributeIntegerRangeDetails>(esqlIntegerAttributeDetails);
var RecipeAttributes = queryAttributeDetails.ToList();
foreach (var AttributeDetails in RecipeAttributes)
{
MinInteger = AttributeDetails.Min;
MaxInteger = AttributeDetails.Max;
}
return Json(result, JsonRequestBehavior.AllowGet);
}
}
I have found the following post on the asp.net forums which handled my first obstacle - passing different named attributes to the same validator. However, in this example the name is passed in generically - I need to have the name in order to query the table to get the applicable min and max ranges.
http://forums.asp.net/t/1625928.aspx/3/10
Can anyone point me in the right direction?
Thanks!
It is called remote validation! Here is an example:
remote validation

Add relationship to many to many in entity framework code first

I want to add a relationship between multiple existing entities and another existing entity. Here is my model:
public class Term
{
public int TermId { get; set; }
public virtual ICollection<SubForm> SubForms { get; set; }
}
public class SubForm
{
public int SubFormId { get; set; }
public virtual ICollection<Term> Terms { get; set; }
}
I have an update repository method as follows:
public IQueryable<Term> GetTerms()
{
IQueryable<Term> query = db.Terms.AsNoTracking();
return query;
}
public Term UpdateTerm(Term term, IEnumerable<Expression<Func<Term, object>>> properties)
{
if (term.TermId == 0)
{
throw new InvalidOperationException("Term does not exist");
}
db.Terms.Attach(term);
if (properties != null)
{
foreach (var selector in properties)
{
string propertyName = Helpers.PropertyToString(selector.Body);
db.Entry(term).Property(propertyName).IsModified = true;
}
}
db.SaveChanges();
return term;
}
Now I assume this would work when I make this call in my service layer:
public void AddFormToTerm(int termId, int formId)
{
var term = termsRepository.GetTerms().FirstOrDefault(t => t.TermId == termId);
var subForms = termsRepository.GetSubForms().Where(t => t.FormId == formId);
//I assume this would work by adding existing forms to an existing term.
foreach (var subForm in subForms)
{
term.SubForms.Add(subForm);
}
termsRepository.UpdateTerm(term, null);
}
Unfortunately, this doesn't get updated, there is nothing in the intermediate table when I checked the database. No exception was also thrown.
Using AsNoTracking in this case is the problem. Without AsNoTracking it will work. You must keep in mind that you can update a many-to-many relationship only with the change tracking mechanism. But in your code the EF context will know about term and the SubForms collection for the first time when you call Attach in your UpdateTerm method. EF does not notice that you did add the SubForms to the term because those entities were not attached to the context (since you used AsNoTracking = "EF, please do not attach to the context!"). But after Attach nothing happened anymore before you called SaveChanges = No change = No database commands.
So removing AsNoTracking (or creating another method or a parameter to load with tracking) is the best option. Everything else will involve ugly "tricks" like this:
public Term UpdateTerm(Term term, ...)
{
//...
// Restore the state before adding the subforms = current state in DB
var tempSubForms = term.SubForms;
term.SubForms = null;
// Inform EF about this state = term exists, subforms exist
// in DB but no relationships
db.Terms.Attach(term);
foreach (var subForm in tempSubForms)
db.SubForms.Attach(subForm);
// Change the state: EF change tracking recognizes this
term.SubForms = tempSubForms;
//...
// EF now will send INSERT statements for the join table
db.SaveChanges();
return term;
}

Resources