ef-code-first

Add-Migration error: Object reference not set to an instance of an object

☆樱花仙子☆ 提交于 2019-12-13 07:06:27
问题 I'm using code-first concept and just tried adding a new class to the Models folder in hope to create a new table in SQL server database. i've added Public Overridable Property Diagnosis As DbSet(Of Diagnosis) in the DBContext.vb. Then in the package manager console, i entered Add-Migration Diagnosis. But below error comes out. Why is this? Can anyone help me out please? Object reference not set to an instance of an object. Full error in Package Manager Console: System.NullReferenceException:

Many to one configuration using EF 4.1 code first

北战南征 提交于 2019-12-13 07:04:12
问题 I have the following classes: public class CartItem { public long Id { get; set; } public int Quantity { get; set; } public Product Product { get; set; } } public class Product { public long Id { get; set; } public string Title { get; set; } public decimal Price { get; set; } } I currently have the following configuration: modelBuilder.Entity<CartItem>().HasRequired(x => x.Product).WithMany().Map(x => x.MapKey("ProductId")); I am trying to ensure that whenever I retrieve a cartitem from the

Repository pattern with EF Code First

北城余情 提交于 2019-12-13 06:16:07
问题 I am not grasping something when creating my repository pattern with EF Code First. If I want to abstract out EF I would have to make my repository be of type IObjectContextAdapter, no? That is what DbContext implements. If I later switch to use something like NHibernate or some other 3rd party ORM, it may not implement IObjectContextAdapter. Is my only solution to create a wrapper that wraps the ORM and does return an implementation of IObjectContextAdapter? If so, what is the point? 回答1: I

EF 4.3 Code First Migrations - Uncompress __MigrationHistory Model

谁说胖子不能爱 提交于 2019-12-13 06:10:47
问题 How can I uncompress/read the compressed models stored in the EF 4.3 Code First Migrations __MigrationHistory table? I found a hint in this comment, that I can uncompress it using zip/gzip, but I don't know how exactly I should do this. The problem is that I have changed something in my ef model classes, but when I call "add-migration" an empty migration ist generated. I think that is because the changes of my model classes are already assigned to an existing migration :( 回答1: (this may be a

EF Codefirst Winforms app - Connection string problems

血红的双手。 提交于 2019-12-13 05:25:31
问题 I've been trying to build a code first desktop app - it works fine on my dev machine, but, I seem to be having so many errors trying to deploy it. The main one at the moment that I can't work out is that when running, I get the following error: Unhandled exception has occurred in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately. Expansion of |DataDirectory| failed while processing

Why is this column getting generated in EF Code-First migrations?

微笑、不失礼 提交于 2019-12-13 04:33:36
问题 I have a Mentorship entity, which has Student and Mentor as FKs: [Required] public int MentorId { get; set; } public virtual User Mentor { get; set; } [Required] public int StudentId { get; set; } public virtual User Student { get; set; } User model: public virtual ICollection<Mentorship> Mentorships { get; set; } Fluent API: modelBuilder.Entity<Mentorship>() .HasRequired(c => c.Mentor) .WithMany() .HasForeignKey(u => u.MentorId); modelBuilder.Entity<Mentorship>() .HasRequired(c => c.Student)

Error when using FromSql and include (“The Include operation is not supported when calling a stored procedure.”)

旧时模样 提交于 2019-12-13 03:32:37
问题 I used Asp.Net Core version 2 and code-first. I tried to use .FromSql to call a stored procedure. I did the same as Microsoft said: var blogs = context.Blogs .FromSql($"SELECT * FROM dbo.SearchBlogs({searchTerm})") .Include(b => b.Posts) .ToList(); and my stored procedure only contains the line of code ALTER PROCEDURE [dbo].[GetCouffierSearch] AS BEGIN SET NOCOUNT ON; SELECT * FROM AspNetUsers END My code in the API: public IQueryable<ApplicationUser> SelectNearByUser() { var query = "execute

EF 0..1 to 1..* relationship

穿精又带淫゛_ 提交于 2019-12-13 02:57:53
问题 Is there a way to create a zero-or-one to one-or-many relationship in Entity Framework? I've seen plenty examples showing a 0..1 to 0..* relationship, but I want to be sure that in the given example Foo can only exist if it has at least one Bar. class Foo { List<Bar> Bars { get; set; } // Must at least have one Bar } class Bar { public Foo Foo { get; set; } // Foo is nullable } I see that this is not easily achieved by SQL since I want a kind of NOT NULL at the Foo table instead of at the Bar

Can not access the composite key table through a view, in order to enter data or assign the correct primary key in that table?

泪湿孤枕 提交于 2019-12-13 02:49:24
问题 I'm trying to develop an ASP.NET MVC application using C# and EF with a code-first approach. I have two classes Actors and Movies which have many-to-many relationships (an Actor can be in many Movies , a Movie can have many Actors ). Everything is working properly, but I can not access the intermediate ( MoviesActors ) table through a view (this table is generated automatically as a result of the many-to-many relationship between Actors and Movies ). I want to access this table so that I

Linq to Entity, selecting group without value

烂漫一生 提交于 2019-12-13 02:49:22
问题 I would like to be able to group all my data by months even if some months doesn't contain any data. At this moment, I can group but the data returned contain only months with data. Here is how my code look like: var twelveMonthAgo = date.AddMonths(-12).Date; var twelveMonthAgoFirstOfMonth = new DateTime(twelveMonthAgo.Year, twelveMonthAgo.Month, 1, 0, 0, 0, 0); var data = (from i in Database.Users where i.RegisterDate >= twelveMonthAgoFirstOfMonth group i by new {y=i.RegisterDate.Year,m = i