SqlException generetad, когда я запускаю свой проект UWP - PullRequest
0 голосов
/ 03 апреля 2019

Я создал проект uwp и проект .NET Standard. .NETproject - создатель моей БД с codeFirst, внутри - моя модель и мой DBContext

Моя модель

public class Department
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int DepartmentId { get; set; }
        public string DepartmentName { get; set; }
        public string DepartmentDescription { get; set; }

        public virtual ICollection<Employee> Employees { get; set; }
    }

public class Employee
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int EmployeeId { get; set; }

        [Required]
        [MaxLength(50)]
        public string EmployeeName { get; set; }
        public int DepartmentId { get; set; }
        public int Salary { get; set; }
        [ForeignKey("DepartmentId")]
        public virtual Department Department { get; set; }
    }

DBContext и метод инициализации

public class EmployeeContext : DbContext
    {
        public DbSet<Department> Departments { get; set; }
        public DbSet<Employee> Employees { get; set; }

        public EmployeeContext(DbContextOptions<EmployeeContext> options) : base(options)
        {
            Database.Migrate();
        }
    }

public class Initialize
    {
        public static EmployeeContext GetContext()
        {
            var connectionString = @"Server=localhost;Database=EFCoreTest;Trusted_Connection=True;";
            DbContextOptionsBuilder<EmployeeContext> options = new DbContextOptionsBuilder<EmployeeContext>();
            options.UseSqlServer(connectionString);
            return new EmployeeContext(options.Options);
        }
    }

В UWP (в app.xaml.cs) я вызываю мой DBContext для создания моей БД в первый раз, но при этом генерируется SqlException

Звонок на мой app.xaml.cs

var context = Initialize.GetContext();
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)

Спасибо за вашу помощь

Ответы [ 2 ]

0 голосов
/ 03 апреля 2019

проверьте возможности вашего приложения uwp, если они активированы.

enter image description here

0 голосов
/ 03 апреля 2019

Ваша ConnectionString неверна. Пожалуйста, проверьте Соединение с помощью visual studio

enter image description here

enter image description here

enter image description here

...