entity-framework

How to I access the DbContext of EF core from another project when used in ASP.NET core?

时光毁灭记忆、已成空白 提交于 2020-03-02 12:27:12
问题 I followed the pattern to use EF Core with ASP.NET core and all is well. But recently I created a 'Calculation' project and want to make database calls from it. The problem is I don't know how to create a new DbContextOptions . In my code that is done with services.AddDbContext<RetContext>(options => options .UseLazyLoadingProxies() .UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); But in a new .NET core class I need to provide it manually. How do I do this ? My code is

EF 6.1 Index IsUnique not being generated

末鹿安然 提交于 2020-03-02 07:22:23
问题 I have the following code first entity: class Role { [Key] public int Id { get; set; } [Index("NameIndex", IsUnique = true)] public string Name { get; set; } } But on checking the database with SQL Management Studio, there are multiple rows of the same name: ID=1, Name=admin ID=2, Name=admin My context is very simple: class MemberContext : DbContext { public DbSet<User> Users { get; set; } public DbSet<Role> Roles { get; set; } } Checking the SQL 2014 Express database table indexes, none are

Constructor on type 'System.Data.Entity.Infrastructure.SqlConnectionFactory' not found

纵饮孤独 提交于 2020-03-01 05:39:25
问题 I "upgraded" my PC from Windows 7 to Windows 8 yesterday, so now I'm using Visual Studio 2012, and opening my Visual Studio 2010 project. This project always worked fine, but it doesn't work in Visual Studio 2012. I had some weird errors, but I fixed those. Now I'm left with one error that I just can't fix and can't find anything about on Google: I get this exception: Failed to set Database.DefaultConnectionFactory to an instance of the 'System.Data.Entity.Infrastructure.SqlConnectionFactory,

How to deal with a text[] array field type in Npgsql?

半腔热情 提交于 2020-02-29 08:04:59
问题 I have a Postgres 12 database with a single table: CREATE TABLE public.messages ( sender text COLLATE pg_catalog."default", "timestamp" timestamp with time zone, message_id bigint, text text COLLATE pg_catalog."default", priority bigint, parameters text[] COLLATE pg_catalog."default" ) Now, when I want to use this table in met .NET Winforms (not .NET Core) application by right-clicking the project, add new item, ADO.NET Entitity Data Model, EF Code First from Database, configuring my

How to deal with a text[] array field type in Npgsql?

不打扰是莪最后的温柔 提交于 2020-02-29 08:04:41
问题 I have a Postgres 12 database with a single table: CREATE TABLE public.messages ( sender text COLLATE pg_catalog."default", "timestamp" timestamp with time zone, message_id bigint, text text COLLATE pg_catalog."default", priority bigint, parameters text[] COLLATE pg_catalog."default" ) Now, when I want to use this table in met .NET Winforms (not .NET Core) application by right-clicking the project, add new item, ADO.NET Entitity Data Model, EF Code First from Database, configuring my

How to deal with a text[] array field type in Npgsql?

百般思念 提交于 2020-02-29 08:04:15
问题 I have a Postgres 12 database with a single table: CREATE TABLE public.messages ( sender text COLLATE pg_catalog."default", "timestamp" timestamp with time zone, message_id bigint, text text COLLATE pg_catalog."default", priority bigint, parameters text[] COLLATE pg_catalog."default" ) Now, when I want to use this table in met .NET Winforms (not .NET Core) application by right-clicking the project, add new item, ADO.NET Entitity Data Model, EF Code First from Database, configuring my

Unit testing for inner exceptions

╄→尐↘猪︶ㄣ 提交于 2020-02-27 14:45:05
问题 I am writing some unit tests using Visual Studio's integrated framework. I need to write some test cases which pass when a proper exception is thrown. The problem is that the exceptions i need to test for are inner exceptions nested in a more general one. Is there some easy solution or do I need to extend the whole functionality. I am currently using the [ExpectedException] attribute, but it wont do much good in such a situation. I am also curious what happens when we use [ExpectedException]

Why does using parameterized queries or entity framework prevent sql injection?

北城余情 提交于 2020-02-27 07:24:32
问题 I've got a good grasp on SQL injection. It's when a SQL query that is supposed to be something like SELECT FirstName, LastName FROM Customers WHERE CustomerId = @valueFromApplication Gets turned into a query like SELECT FirstName, LastName FROM Customers WHERE CustomerId = '' ; DROP DATABASE Foo -- When the user inserts a malicious value into your app, website, client, whatever.. I'm also aware that instead of just dropping the DB the attacker can try to discover the names of tables and get

How to use System.Lazy with Setter to Lazy Initialization of List in POCO Entities?

冷暖自知 提交于 2020-02-26 11:31:15
问题 I want to use System.Lazy to Lazy Initialization of my List in my Entites: public class Questionary { private Lazy<List<Question>> _questions = new Lazy<List<Question>>(() => new List<Question>()); public IList<Question> Questions { get { return _questions.Value; } set { _questions.Value = value; } } } The problem is on my SETTER, get this error: The property ' System.Lazy<T>.Value ' has no setter If i want to do MyInstance.Questions = new List<Question> { ... } ? How do I proceed? Update: I

The term 'scaffold-dbcontext' is not recognized as the name of a cmdlet, function, script file, or operable program

有些话、适合烂在心里 提交于 2020-02-26 10:49:46
问题 When trying to scaffold with asp.net core this command scaffold-dbcontext "Data Source=(local);Initial Catalog=MyDb;Integrated Security=True;" Microsoft.EntityFrameworkCore.sqlserver -outputdir Models Gives this error. scaffold-dbcontext : The term 'scaffold-dbcontext' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + scaffold