have been trying to get to grips with the entity framework using the music store tutorial as a base reworking it to fit an existing application i have
i have a database table called AuthorDetail. I also have AuthorDetail.cs Model, and a View AuthorDetail.cshtml strongly-typed to the Model. It's also declared as DBSet in the Entities class :DbContext under the application Models namespace.
the model is:
public
class
AuthorDetail
{
publicGuid
UsID { get;
set;
}
publicint
Gender { get;
set;
}
publicstring
FirstName { get;
set;
}
publicstring
LastName { get;
set;
}
publicint
DDOB { get;
set;
}
publicint
MDOB { get;
set;
}
publicint
YDOB { get;
set;
}
publicint
UsePenName { get;
set;
}
publicstring
PenFirstName { get;
set;
}
publicstring
PenLastName { get;
set;
}
publicstring
Address1 { get;
set;
}
publicstring
Address2 { get;
set;
}
publicstring
City { get;
set;
}
publicstring
PostCode { get;
set;
}
publicint
PhoneType { get;
set;
}
publicstring
PhoneNumb { get;
set;
}
publicstring
Description { get;
set;
}
}
the controller is:
iLiteratiEntities
iLitAuth = newiLiteratiEntities
();
publicActionResult
Index()
{
return
View();
}
publicActionResult
AuthorDetail()
{
var
authors = iLitAuth.AuthorDetail.ToList();
returnView(authors);
}
when i build the page i get the error: entity type 'Author Detail' has no key defined (actually i get this error for every entity type i set.)
am i missing something?
Please be patient if say anything really dumb, I'm self-taught but I'm trying!
I'll always mark as answer when you help me - and if I can help you please do the same :)
if iLitAuth is a context and AuthorDetail is a table defined here, the above is wrong....you can't transform the whole table into a list. The right way is:
(select x in iLitAuth.AuthorDetail).ToList();
(select x in iLitAuth.AuthorDetail) returns an IQueryable
that you can then transform into a list.
ok thank you, that makes sense, but i am still not getting anything from my db
i have a table 'manusctripts' where details of books uoploaded into my application are stored.
on the controller i have:
var rmss = ilitAuth.Manuscripts.ToList();
return View(rmss);
and in the view i have:
@model IEnumerable<iliteratimvc.Models.Manuscripts>
@{
ViewBag.Title = "RecentMSS";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Manuscripts</h2>
@foreach (var m in Model)
{
<li>@m.MsTitle </li>
}
when i run the page, i get an error "Object reference not set to an instance of an object." at the @foreach (var m in model) part of my code in the View. However I know for a fact that there are plenty of entries in that table and so this error shouldn't
occur. is that because of the reason you have set out above?
in which case how do i do this?
Please be patient if say anything really dumb, I'm self-taught but I'm trying!
I'll always mark as answer when you help me - and if I can help you please do the same :)
unfortunately i am still getting the same error. is there any reason why it can see / find the database schema but not can the data?
i am sure it can see the schema because i get the correct intellisense prompts so i know it's seeing the right database tables, e.g. when writing the view above when i type @m. i get the intellisense showing me all the columns in the manuscript table, but
then the page isn't getting any data hence the object reference error.
i'm really at a loss!
Please be patient if say anything really dumb, I'm self-taught but I'm trying!
I'll always mark as answer when you help me - and if I can help you please do the same :)
you set the key manually, so if you used the designer for EF you will loose all changes when using again the designer because the designer will create a new file from scratch when do any change.
Define then key IN THE DESIGNER, and then try an updatefromdatabase (right click on the designer), then choose refresh.
If this doesnt work maybe while working you created two databases with two connection strings ..verify this in the configuaration file and then see also if the connection used bya the file created by the designer is the right one.
if this also doesn't solve, then debug the code and put a breakpont just after the ToList to see if the list is populated ...or what happens
there are indeed two connection strings in the web.config, one i created manually when setting the membership provider etc and the other was generated when i created the .edmx file. however both point to the same database.
i tried removing the one i created myself, but the same error results with the added error that I can't use the login anymore (i changed the role providers but still got an error in the accountmodels.cs file). (however, please ignore the login error, i don't
want to get sidetracked)
i didn't define keys manually in the end, but decided to use the .edmx file
i did the debug / breakpoint you suggested, but i am not sure how to tell if the list is actually popoulated? where would the output be?
i am completely lost and have been stuck for ages. i can't believe it's so difficult to get the data out, i'm sure i'm missing something obvious, but all the tutorials on mvc use .mdf files in the AppData folder which is not how i'm using this and not a
realistic scenario. thanks for your help and patience!
Please be patient if say anything really dumb, I'm self-taught but I'm trying!
I'll always mark as answer when you help me - and if I can help you please do the same :)
dcgate
Member
312 Points
192 Posts
entity type has no key defined error - help please!
Mar 28, 2011 12:14 PM|LINK
have been trying to get to grips with the entity framework using the music store tutorial as a base reworking it to fit an existing application i have
i have a database table called AuthorDetail. I also have AuthorDetail.cs Model, and a View AuthorDetail.cshtml strongly-typed to the Model. It's also declared as DBSet in the Entities class :DbContext under the application Models namespace.
the model is:
public
classAuthorDetail
{
public Guid UsID { get; set; }
public int Gender { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int DDOB { get; set; }
public int MDOB { get; set; }
public int YDOB { get; set; }
public int UsePenName { get; set; }
public string PenFirstName { get; set; }
public string PenLastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string PostCode { get; set; }
public int PhoneType { get; set; }
public string PhoneNumb { get; set; }
public string Description { get; set; }}
the controller is:
iLiteratiEntities iLitAuth = new iLiteratiEntities();
public ActionResult Index(){
return View();}
public ActionResult AuthorDetail(){
var authors = iLitAuth.AuthorDetail.ToList();
returnView(authors);}
when i build the page i get the error: entity type 'Author Detail' has no key defined (actually i get this error for every entity type i set.)
am i missing something?
I'll always mark as answer when you help me - and if I can help you please do the same :)
Dejan_S
Member
413 Points
202 Posts
Re: entity type has no key defined error - help please!
Mar 28, 2011 12:58 PM|LINK
Try setting [Key] on the key property
dcgate
Member
312 Points
192 Posts
Re: entity type has no key defined error - help please!
Mar 28, 2011 02:11 PM|LINK
thanks - how/where do i do this?
I'll always mark as answer when you help me - and if I can help you please do the same :)
chohmann
Star
9385 Points
1644 Posts
Re: entity type has no key defined error - help please!
Mar 28, 2011 05:26 PM|LINK
Directly above the member definition:
[Key] public Guid UsID { get; set; }francesco ab...
All-Star
20888 Points
3277 Posts
Re: entity type has no key defined error - help please!
Mar 28, 2011 06:49 PM|LINK
what is this :
iLitAuth.AuthorDetail.ToList();
if iLitAuth is a context and AuthorDetail is a table defined here, the above is wrong....you can't transform the whole table into a list. The right way is:
(select x in iLitAuth.AuthorDetail).ToList();
(select x in iLitAuth.AuthorDetail) returns an IQueryable that you can then transform into a list.
Mvc Controls Toolkit | Data Moving Plug-in Videos
dcgate
Member
312 Points
192 Posts
Re: entity type has no key defined error - help please!
Apr 04, 2011 12:07 PM|LINK
ok thank you, that makes sense, but i am still not getting anything from my db
i have a table 'manusctripts' where details of books uoploaded into my application are stored.
on the controller i have:
var rmss = ilitAuth.Manuscripts.ToList(); return View(rmss);and in the view i have:
@model IEnumerable<iliteratimvc.Models.Manuscripts> @{ ViewBag.Title = "RecentMSS"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Manuscripts</h2> @foreach (var m in Model) { <li>@m.MsTitle </li> }when i run the page, i get an error "Object reference not set to an instance of an object." at the @foreach (var m in model) part of my code in the View. However I know for a fact that there are plenty of entries in that table and so this error shouldn't occur. is that because of the reason you have set out above?
in which case how do i do this?
I'll always mark as answer when you help me - and if I can help you please do the same :)
francesco ab...
All-Star
20888 Points
3277 Posts
Re: entity type has no key defined error - help please!
Apr 04, 2011 12:43 PM|LINK
try:
Mvc Controls Toolkit | Data Moving Plug-in Videos
dcgate
Member
312 Points
192 Posts
Re: entity type has no key defined error - help please!
Apr 04, 2011 02:03 PM|LINK
thanks for your help.
unfortunately i am still getting the same error. is there any reason why it can see / find the database schema but not can the data?
i am sure it can see the schema because i get the correct intellisense prompts so i know it's seeing the right database tables, e.g. when writing the view above when i type @m. i get the intellisense showing me all the columns in the manuscript table, but then the page isn't getting any data hence the object reference error.
i'm really at a loss!
I'll always mark as answer when you help me - and if I can help you please do the same :)
francesco ab...
All-Star
20888 Points
3277 Posts
Re: entity type has no key defined error - help please!
Apr 04, 2011 02:52 PM|LINK
First,
you set the key manually, so if you used the designer for EF you will loose all changes when using again the designer because the designer will create a new file from scratch when do any change.
Define then key IN THE DESIGNER, and then try an updatefromdatabase (right click on the designer), then choose refresh.
If this doesnt work maybe while working you created two databases with two connection strings ..verify this in the configuaration file and then see also if the connection used bya the file created by the designer is the right one.
if this also doesn't solve, then debug the code and put a breakpont just after the ToList to see if the list is populated ...or what happens
Mvc Controls Toolkit | Data Moving Plug-in Videos
dcgate
Member
312 Points
192 Posts
Re: entity type has no key defined error - help please!
Apr 04, 2011 03:24 PM|LINK
there are indeed two connection strings in the web.config, one i created manually when setting the membership provider etc and the other was generated when i created the .edmx file. however both point to the same database.
i tried removing the one i created myself, but the same error results with the added error that I can't use the login anymore (i changed the role providers but still got an error in the accountmodels.cs file). (however, please ignore the login error, i don't want to get sidetracked)
i didn't define keys manually in the end, but decided to use the .edmx file
i did the debug / breakpoint you suggested, but i am not sure how to tell if the list is actually popoulated? where would the output be?
i am completely lost and have been stuck for ages. i can't believe it's so difficult to get the data out, i'm sure i'm missing something obvious, but all the tutorials on mvc use .mdf files in the AppData folder which is not how i'm using this and not a realistic scenario. thanks for your help and patience!
I'll always mark as answer when you help me - and if I can help you please do the same :)