标签: ef-code-first

如何在 Entity Framework Code First 中使属性唯一

这是我的User模型类:

public class User 
{
    public int Id { get; set; }

    [Required]
    [MaxLength(100)]
    public string FullName { get; set; }

    [Required]
    [MaxLength(30)]
    [Remote("IsUsernameExists", "Home", HttpMethod = "Post")]
    public string UserName { get; set; }

    [Required]
    [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$"]
    public string Email { get; set; }

    [Required]
    [StringLength(maximumLength: 18, MinimumLength = 8)]
    public string Password { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是控制器IsUsernameExists中的方法Home

public JsonResult IsUsernameExists(string UserName)
{            
    return Json(!db.Users.Any(x => x.Username == UserName), JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用。任何进一步的帮助将不胜感激!

c# linq json entity-framework ef-code-first

1
推荐指数
1
解决办法
4082
查看次数

System.InvalidOperationException:在 SqlServer 提供程序清单中找不到存储类型“decimal(18,4)”

我正在按以下方式构建连接字符串(使用适当的连接参数值)。

var builder = new SqlConnectionStringBuilder
{
    PersistSecurityInfo = false,
    InitialCatalog = "mydatabase",
    UserID = "myuser",
    Password = "mydatabase",
    DataSource = "myserver"
};
var connectionString = builder.ConnectionString;
using (var db = Helper.MakeContext(connectionString)) {
    var carrier = db.Carriers.FirstOrDefault(); // fails here with error
    Console.WriteLine(carrier.Carrier);
}
Run Code Online (Sandbox Code Playgroud)

Helper 在不同的项目中

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using ApiForVivtrack3.Entities;
    
public static class Helper
{
    //public  string ConnectionString { get; set; }
    public static ApiDbContext MakeContext(string connectionString)
    {
        var db = new …
Run Code Online (Sandbox Code Playgroud)

ef-code-first

1
推荐指数
1
解决办法
2416
查看次数

F# - 合并模块?

我正在为F#开发一个实体框架代码第一包装器,我一直在想我是否应该将所有模块合并为一个.

看看这个:

module ManyNavProperty =
    let withMany (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithMany()
    let withSeq expr (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithSeq expr
    let withList expr (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithList expr
    let withArray expr (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithArray expr
    let withOptional (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithOptional()
    let withOptionalProperty expr (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithOptional expr
    let withRequired (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithRequired()
    let withRequiredProperty expr (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithRequiredProperty expr

module DependentNavProperty =
    let hasForeignKey expr (cfg:DependentNavPropertyInfo<'a>) = cfg.HasForeignKey expr

module CascadableNavProperty =
    let willCascadeOnDelete b (cfg:CascadableNavPropertyInfo) = cfg.WillCascadeOnDelete b

module EF =
    let entity<'a …
Run Code Online (Sandbox Code Playgroud)

f# entity-framework ef-code-first

0
推荐指数
1
解决办法
195
查看次数

Code First Entity Framework连接字符串,Data Source关键字错误

试图让我的第一个MVC + EF代码第一个项目运行.该数据库尚不存在.这是我的连接字符串,我的模型名为FirstDB:

<add name="FirstDB" 
     connectionString="Data Source=.; Initial Catalog=First; Integrated Security=SSPI; Provider=SQLNCLI10.1;" 
     providerName="System.Data.EntityClient"/>
Run Code Online (Sandbox Code Playgroud)

我得到的错误说数据源不是一个公认的关键字.

http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

这是我正在关注的指南,但不幸的是,他们使用的是sdf文件,而我发现的所有其他示例都是在用户模式下使用mdf文件.我不想做其中任何一个.

如果要看到它是非常重要的,我的问题是:如何消除错误,以便我可以连接到数据库服务器并让EF代码首先生成我的数据库?

entity-framework ef-code-first

0
推荐指数
1
解决办法
1621
查看次数

如何在ASP.NET MVC中映射许多一对多关系?

我有几个领域模型- ,Address,Customer,.Employee 有多对一的关系,并与StoreLocation一一对应的关系.StoreLocationAddressCustomerEmployee

public class Address
{
    public int Id;
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string Line3 { get; set; }
}

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IList<Address> Addresses { get; set; }
}

public class StoreLocation
{
    public int Id { get; set; }
    public string …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework one-to-many ef-code-first

0
推荐指数
1
解决办法
1万
查看次数

首先学习EF代码:与模型相比有哪些缺点?

我正在考虑首先从EF模型转向代码.对我来说,这些优势看起来很清晰,而且使用起来似乎非常直观.与模型相比有哪些缺点?我必须怀疑哪些陷阱?

ef-code-first entity-framework-4.1

0
推荐指数
1
解决办法
737
查看次数

查找少于n个子实体的实体

我试图找到一种简单的方法来挑选一个客户服务代表来分配给一个给定的用户.我有以下型号:

public class Customer
{
    public int Id { get; set; }
    // SNIP
    public virtual Representative Representative { get; set; }
    public bool Active { get; set; }
}

public class Representative
{
    public int Id { get; set; }
    public int MaxActiveCustomers { get; set; }
    // all the customers this representative has interacted with
    public IEnumerable<Customer> Customers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我试图找到目前CustomersMaxActiveCustomers建议少的代表.

我的尝试:

from r in Representatives
where r.MaxActiveCustomers > r.Customers.Count(c => …
Run Code Online (Sandbox Code Playgroud)

c# linq entity-framework ef-code-first

0
推荐指数
1
解决办法
129
查看次数

实体框架代码第一个默认值为bit数据类型

使用EntityFramework codefirst的最新版本(4.3),我创建了一个包含Enabled(bit)列的表.然后我发现它的默认值为false,我可以将默认值设置为true吗?

当我将列重命名为Disabled时,我发现了一些奇怪的东西,默认值变为null.

entity-framework ef-code-first

0
推荐指数
1
解决办法
5446
查看次数

ASP.NET MVC EF Code First中的一对一或非一对一关系

我是.NET MVC的新手,我正在努力将Code First与现有数据库结合使用,其中一个表具有一对一或一对(1 - > 0..1)的关系.

我有一份报告,可以有很多部分,每个部分都有很多问题.现在这里有点我认为我遇到了麻烦...每个问题可能有一个答案或没有答案.

我收到以下错误:

System.Data.Edm.EdmAssociationEnd :: Multiplicity在关系'QuestionAnswer_Question'中的角色'QuestionAnswer_Question_Source'中无效.由于Dependent Role属性不是关键属性,因此Dependent Role的多重性的上限必须为*.

这是我的模型类:

ModeratorReport.cs

public class ModeratorReport
{
    [Key, Column(Order = 0)]
    public int ModeratorReportID { get; set; }

    [Key, Column(Order = 1)]
    public string Status { get; set; }

    public string FileYear { get; set; }
    public string SessionCode { get; set; }
    public string CentreNumber { get; set; }
    public string SubjectNumber { get; set; }
    public string PaperNumber { get; set; }
    public string ModeratorNumber { …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework ef-code-first

0
推荐指数
1
解决办法
3636
查看次数

用于导航属性的Fluent API有两种方式

public class ProductType
{
    public Guid ID { get; set }
    public string Name { get; set }
    public ICollection<ProductCategory> Categories { get; set }
}

public class ProductCategory
{
    public Guid ID { get; set }
    public string Name { get; set }
    public ProductType ProductType { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

组态

ProductTypeConfiguration

HasMany(p => p.Categories).WithRequired().WillCascadeOnDelete(false);
Run Code Online (Sandbox Code Playgroud)

注意属性CategoriesProductType

关系是一个(ProductType)到很多(ProductCategory),但是,a ProductCategory与单个关联ProductType!

在我的数据库中它创建了两个FKS !! 如何配置(使用FluentAPI)这种情况?

谢谢!!!

fluent-interface fluent entity-framework-4 ef-code-first

0
推荐指数
1
解决办法
1109
查看次数