Note that there are some explanatory texts on larger screens.

plurals
  1. POentity framework 4.1 invalid column name
    primarykey
    data
    text
    <p>I have two table News and NewsComments. I followed the rules of naming</p> <p>structure NewsComments</p> <pre><code>public class NewsComment : BaseComment { public int NewsId { get; set; } public virtual News News { get; set; } } </code></pre> <p>But query return exception Invalid column name "News_Id". I know what this exception created when in table not related column.</p> <pre><code>CREATE TABLE [dbo].[NewsComments]( [Id] [int] IDENTITY(1,1) NOT NULL, [NewsId] [int] NOT NULL, [Text] [varchar](max) NOT NULL, [UserId] [int] NOT NULL, [CommentDate] [datetime] NOT NULL, [Ip] [varchar](40) NOT NULL, CONSTRAINT [PK_NewsComments] PRIMARY KEY CLUSTERED([Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] </code></pre> <p><strong>BaseComment</strong></p> <pre><code>public abstract class BaseComment : BasePersistentEntity, IComment { public int UserId { get; set; } public virtual BaseUser User { get; set; } [Display(ResourceType = typeof(FrameworkResurce), Name = "CommentText")] public string Text { get; set; } [Display(ResourceType = typeof(FrameworkResurce), Name = "CommentDate")] public DateTime CommentDate { get; set; } public string Ip { get; set; } } </code></pre> <p><strong>News</strong></p> <pre><code>public class News : BaseContent { [Display(ResourceType = typeof(NewsResurce), Name = "NewsImage")] public string NewsImage { get; set; } public virtual ICollection&lt;NewsCommentView&gt; CommentViews { get; set; } } </code></pre> <p><strong>BaseContent</strong></p> <pre><code>public abstract class BaseContent : BasePersistentEntity { [Display(ResourceType = typeof(FrameworkResurce), Name = "Keywords")] public string Keywords { get; set; } [Display(ResourceType = typeof(FrameworkResurce), Name = "TitleTranslit")] public string TitleTranslit { get; set; } [Required(ErrorMessageResourceType = typeof(FrameworkResurce), ErrorMessageResourceName = "IsTextEmpty")] [Display(ResourceType = typeof(FrameworkResurce), Name = "Title")] public string Title { get; set; } [Display(ResourceType = typeof(FrameworkResurce), Name = "Description")] public string Description { get; set; } [Display(ResourceType = typeof(FrameworkResurce), Name = "Contents")] public string Contents { get; set; } [Display(ResourceType = typeof(FrameworkResurce), Name = "DatePublish")] public DateTime DatePublish { get; set; } [Display(ResourceType = typeof(FrameworkResurce), Name = "AuthorPublish")] public string AuthorPublish { get; set; } [Display(ResourceType = typeof(FrameworkResurce), Name = "Author")] public string Author { get; set; } [Display(ResourceType = typeof(FrameworkResurce), Name = "AuthorUrl")] public string AuthorUrl { get; set; } [Display(ResourceType = typeof(FrameworkResurce), Name = "Views")] public int Views { get; set; } [Display(ResourceType = typeof(FrameworkResurce), Name = "Comments")] public int Comments { get; set; } [Display(ResourceType = typeof(FrameworkResurce), Name = "IsComment")] public bool IsComment { get; set; } [Display(ResourceType = typeof(FrameworkResurce), Name = "SumVote")] public int SumVote { get; set; } [Display(ResourceType = typeof(FrameworkResurce), Name = "VoteCount")] public int VoteCount { get; set; } [NotMapped] [Display(ResourceType = typeof(FrameworkResurce), Name = "Rating")] public double Rating { get { if (VoteCount &gt; 0) { return Math.Round((float)SumVote/VoteCount, 2); } return 0; } } } </code></pre> <p><strong>Query</strong></p> <pre><code>private IEnumerable&lt;NewsComment&gt; GetComments() { var news = NewsCommentRepository.AllIncluding(c=&gt;c.User,c=&gt;c.News); return news; } private DataRepository&lt;NewsComment&gt; NewsCommentRepository { get { return DataRepository&lt;NewsComment&gt;.Repository; } } </code></pre> <p><strong>DataRepository</strong></p> <pre><code>public class DataRepository&lt;T&gt; where T : BasePersistentEntity { public static DataRepository&lt;T&gt; Repository { get { return new DataRepository&lt;T&gt;(); } } private readonly SGNContext&lt;T&gt; context = new SGNContext&lt;T&gt;(); public IQueryable&lt;T&gt; All { get { return this.context.Table; } } public IQueryable&lt;T&gt; AllIncluding(params Expression&lt;Func&lt;T, object&gt;&gt;[] includeProperties) { IQueryable&lt;T&gt; query = this.context.Table; return includeProperties.Aggregate(query, (current, includeProperty) =&gt; current.Include(includeProperty)); } public T Find(int id) { return this.context.Table.Find(id); } public void InsertOrUpdate(T country) { if (country.Id == default(int)) { // New entity this.context.Table.Add(country); Save(); } else { // Existing entity this.context.Entry(country).State = EntityState.Modified; Save(); } } public void Delete(int id) { var country = this.context.Table.Find(id); this.context.Table.Remove(country); this.Save(); } private void Save() { this.context.SaveChanges(); } } </code></pre> <p><strong>Where used GetComments</strong></p> <pre><code> [GridAction] public ActionResult AjaxCommentsBinding() { return View(new GridModel&lt;NewsComment&gt; { Data = GetComments() }); } </code></pre> <p><strong>NewsCommentViews</strong></p> <pre><code>CREATE VIEW [dbo].[NewsCommentViews] AS SELECT dbo.NewsComments.NewsId, dbo.NewsComments.Text, dbo.NewsComments.UserId, dbo.NewsComments.CommentDate, dbo.NewsComments.Ip, dbo.Roles.RoleName, dbo.Users.UserName, dbo.Users.DateRegistered, dbo.NewsComments.Id, dbo.Users.Avatar FROM dbo.NewsComments INNER JOIN dbo.Users ON dbo.NewsComments.UserId = dbo.Users.Id INNER JOIN dbo.Roles ON dbo.Users.RoleId = dbo.Roles.Id </code></pre> <p><strong>NewsCommentViews</strong></p> <pre><code>[Table("NewsCommentViews")] public class NewsCommentView : NewsComment { public string RoleName { get; set; } public string UserName { get; set; } public DateTime DateRegistered { get; set; } public string Avatar { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload