I'm getting the following error:
Error 1 The type or namespace name 'keyAttribute' could not be found
(are you missing a using directive or an assembly reference?)
enter code here
c:\users\hp\documents\visual studio
2013\Projects\ProductApps\ProductApps\Models\Product.cs 13 10 ProductApps
My code:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace ProductApps.Models
{
public class Product
{
[key]
public int Id { get; set; }
public string ProductName { get; set; }
public string Price { get; set; }
public string Description { get; set; }
}
}
Is anyone able to suggest why I'm getting the error and how to fix it?
Please add
using System.ComponentModel.DataAnnotations;
to your using statements and try like (With capital K)
[Key]
public int Id { get; set; }
Do you have the System.ComponentModel.Annotations.dll assembly referenced in your project?
Related
When i am trying to create a speaker class in mvc(asp.net).. a message is displayed..
"The type or namespace Speaker is missing "
how to resolve this???
this is my controller file...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace white.Models
{
public class session
{
public Int32 session_id { get; set; }
public String title { get; set; }
public String description { get; set;}
public Int32 SpeakerId { get; set; }
public Speaker speaker { get; set; }
}
}
It would appear that Speaker isn't in white.Models and isn't in any of the system namespaces you're referencing. You'll need to add a using statement referencing its namespace (or use its fully-qualified name). If you're not sure what namespace it's in, either look at its documentation (if it's not your class) or look at the Speaker.cs file (if it is).
I have a dbml file that has stored procedures dragged off. I have an EmployeeModel class that has get and set properties.
I have an interface IEmployee and an EmployeeRepository repository that has the implementation of the methods. Please refer to the code. In the stored procedure GetRoles I just have SELECT * FROM ROLE.
In the repository, how do I loop through the resultset? Can I change ISingleResult to IMultipleResult in the dbml designer file?
EmployeeModel.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcWebsite.Models
{
public class EmployeeModel
{
public int RoleId { get; set; }
public string RoleName { get; set; }
public string Description { get; set; }
public string TaskMark { get; set; }
public int RoleFlag { get; set; }
}
}
IEmployee:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcWebsite.Models;
namespace MvcWebsite.DAL
{
public interface IEmployees
{
IList<EmployeeModel> ListAll();
// void Save(EmployeeModel employ);
}
}
EmployeeRepository.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcWebsite.Models;
using System.Data.Linq;
namespace MvcWebsite.DAL
{
public class EmployeeRepository:IEmployees
{
private DataDataContext _dataContext;
public EmployeeRepository()
{
_dataContext = new DataDataContext();
}
public IList<EmployeeModel> ListAll()
{
//IMultipleResults result =_dataContext.GetRoleDetails();
//var Emps = result.GetResult(EmployeeModel);
List<EmployeeModel> emp = _dataContext.GetRoleDetails();
// foreach (GetRoleDetailsResult role in Emps)
// {
// role.Description=Emps.
// }
return Emps.ToList();
}
}
}
Error:
Error 1 Cannot implicitly convert type
'System.Collections.Generic.List'
to
'System.Collections.Generic.IList'.
An explicit conversion exists (are you missing a
cast?) C:\Users\guhananth\documents\visual studio
2010\Projects\MVC_SP\MvcWebsite\DAL\EmployeeRepository.cs 44 19 MvcWebsite
Typicall you would use:
List<EmployeeModel> emp = _dataContext.Employees.ToList();
You can read about a Entity Framework implementation of the Repository pattern here: http://blog.gauffin.org/2013/01/repository-pattern-done-right/
I am trying to make an application where in I have made a Location model,controller and views using scaffolding. I have location name and location ID properties in location. Database is getting created and location table also and data is getting populated. Now i want to make a department class where I have 3 properties namely Dept_ID, Dept_Name and Loc_ID(Foreign key). I have added required codes in respective files as following.
in Department.cs(Model)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace DMS.Models
{
public class Department
{
public int D_ID { get; set; }
public string D_Name { get; set; }
[ForeignKey("L_ID")]
public int L_ID { get; set; }
}
}
in DB_CONTEXT class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace DMS.Models
{
public class DB_CONTEXT : DbContext
{
public DbSet<Location> Movies { get; set; }
public DbSet<Department> Department { get; set; }
}
}
and in locatoin.cs(Model)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace DMS.Models
{
public class Location
{
public int ID { get; set; }
public string L_Name { get; set; }
}
}
when i am trying to add a controller for Department I am getting an error as
unable to retrieve metadata for 'DMS.Models.Department' The navigation property 'L_ID' is not declared property on type Department.Verify that it has not been explicitly excluded from the model and that it is a valid navigation property.
[ForeignKey("LoactionID")]
public int L_ID { get; set; }
public Virtual Location Location {get;set;}
Make these changes in Department model and try once again. I hope this will solve your issue.
I am practicing Entity Framework. I am practicing Asp.net MVC framework using Razor Engine and EF Code First. In practicing it, I found to have few problems. The problem is the Entity Framework doesnt creates a database in my SQL.
I am using VisualStudio 2011 Beta Version with MSSQL 2008. I dont know what the problem is.
Help required. Below is my Code :
Person.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
namespace MvcDemo.Data
{
public class Person
{
[Key]
public int PersonID { get; set; }
[Required]
[StringLength(40)]
public string FirstName { get; set; }
[Required]
[StringLength(30)]
public int LastName { get; set; }
public int? Age { get; set; }
[ForeignKey("PrefixID")]
public Prefix Prefix { get; set; }
[Required]
public int PrefixID { get; set; }
}
}
Prefix.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
namespace MvcDemo.Data
{
public class Prefix
{
public int PrefixID { get; set; }
[Required]
[StringLength(20)]
public string PrefixName { get; set; }
}
}
MvcContext.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using MvcDemo.Data;
namespace MvcDemo.DataAccess
{
public class MvcContext : DbContext
{
public MvcContext()
: base("name = MvcDemoApp")
{
}
public DbSet<Person> People { get; set; }
public DbSet<MvcDemo.Data.Prefix> Prefix { get; set; }
}
}
ConnectionString:
<connectionStrings>
<add name="MvcDemoApp"
connectionString="Server=.;Initial Catalog=MvcDemoApp;Integrated Security=true"
providerName="System.Data.SqlClient" />
If this is all of your code it is not surprising that a database is not created. There is no code that executes commands against the database. As soon as you start iterating over e.g. MvcContext.People or do some insert you will notice that the database gets created.
There are plenty step-by-step examples online. I would pick one of them and do a walk through; you can try this one for example: http://msdn.microsoft.com/en-us/data/gg685467.aspx. It should cover most of the basics to get you started.
If you prefer video over text, search Channel 9, or pluralsight
You should operate package manager console to do it.
Ope package manager console window and run "update-database" command, there still a bug in this command so you need to specify "startupprojectname" param to execute it and connection string in you DbContext file.
I'm using code-first in Web Forms (not MVC) with EF4 and CTP5, and when trying to decorate a property with the [Key] attribute, it doesn't show up in the intellisense and get compilation error saying KeyAttribute was not found. Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace ERP.Models
{
public class CustomerAddress
{
[Key]
public int AddressID { get; set; }
public int CustomerID { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public int CityID { get; set; }
public int SateID { get; set; }
}
}
I have included the DataAnnotations library, all looks ok, but the [Key] attribute is not found. Any hint is highly appreciated. Thanks!
I didn't post it as answer first because I wasn't sure if the problem is not elsewhere. You must add System.ComponentModel.DataAnnotations.dll to project references.