不包含' HasAlternateKey'的定义(EF 6,Code-First,VS2015)

时间:2017-08-17 08:49:07

标签: c# visual-studio entity-framework ef-code-first code-first

大家好,这是我的第一个问题,所以我会尽我所能。 过去几周,我一直在使用Code-First探索实体框架和数据库编程的用途。

现在我正在尝试将备用密钥合并到我的模型中,但我认为将列设为索引和唯一性就足够了,但它并不是要让它们成为另一个表中的FK。< / p>

我找到了一种使用Fluent API声明AK的方法,这非常简单,但是当我尝试使用它时;

modelBuilder.Entity<TypeBus>().HasAlternateKey(c => c.buscode)

但Visual Studio说:EntityTypeConfiguration&lt; TypeBus&gt;不包含&#39; HasAlternateKey&#39;的定义接受类型&#39; EntityTypeConfiguration&lt;的第一个参数TypeBus&gt;&#39;可以找到。

这是我的一段代码总结:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Example_Bus.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity.Infrastructure.Annotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Example_Bus.DAL
{
public class SystemContext : DbContext
{

    public SystemContext() : base("SystemContext")
    {
    }

    public DbSet<TypeBus> TypeBuses { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Entity<Student>().MapToStoredProcedures(s => s.Update(u => u.HasName("modify_student")));
        modelBuilder.Entity<Venedor>().Property(e => e.personaId).HasColumnName("venedorId");
        modelBuilder.Entity<Bus>().MapToStoredProcedures();
        modelBuilder.Entity<Conductor>().MapToStoredProcedures();

        modelBuilder.Entity<TypeBus>().HasAlternateKey(c => c.buscode);

    }

我不确定我是否错过了&#34;使用&#34;什么东西,但我无法在任何地方找到任何东西。 我会感激任何帮助^^ 谢谢你。

1 个答案:

答案 0 :(得分:0)

您可以创建唯一索引。与备用键相同。

modelBuilder.Entity<YourTable>().HasIndex(x => new { x.AKField1, x.AKField2 }).IsUnique();

modelBuilder.Entity<esc20_GrupoClase>().HasIndex(x => new { x.clase_uid, x.grupo_uid }).IsUnique();