小编Bob*_*Bob的帖子

如何在ASP.NET MVC 5,Entity Framework 6中使用流畅的API映射表?

我正在尝试使用带有内置用户身份验证的ASP.NET MVC 5在Entity Framework 6中使用C#创建一对一的关系.

我能够使用Entity Framework创建的默认值创建表和连接.但是当我尝试使用流畅的API时...更具体地说,当我在模型创建上使用甚至是空的时,我使用包管理器控制台迁移数据库将失败.我如何映射我的一对一关系?

我的错误:

//error
//my.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined.   //Define the key for this EntityType.
//my.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. //Define the key for this EntityType.
//IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type    //'IdentityUserLogin' that has no keys defined.
//IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type //'IdentityUserRole' that has no keys defined.
Run Code Online (Sandbox Code Playgroud)

我的代码:

namespace my.Models
{

    public class ApplicationUser : IdentityUser
    {
    }

    public class ApplicationDbContext : …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework entity-framework-6 asp.net-mvc-5 asp.net-identity

11
推荐指数
2
解决办法
3668
查看次数

如何以编程方式在Entity Framework 6中为MS SQL创建连接字符串?

如何以编程方式在Entity Framework 6中为MS SQL创建连接字符串?

我正在使用c#和WPF,我想知道是否有人可以告诉我如何或链接我一个资源,显示如何在EF 6中以编程方式设置连接字符串.MSDN文章解释说你可以http://msdn.microsoft #com/en-us/data/jj680699#moving但它不会创建实际的连接字符串.

所以这是一个有效的EF6示例

App.Config中

entityFramework codeConfigurationType ="WPFwithEF.SqlConfiguration,WPFwithEF">/entityFramework

上下文

public class ProductContext : DbContext
{  
    public ProductContext():base("Wpf")
    { }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Configuration.cs

namespace WPFwithEF
{
public class SqlConfiguration : DbConfiguration
{

    public SqlConfiguration()
    {
        SetProviderServices(SqlProviderServices.ProviderInvariantName,SqlProviderServices.Instance);           
        SetDefaultConnectionFactory(new SqlConnectionFactory());
    }
}
}
Run Code Online (Sandbox Code Playgroud)

但如果上下文基础是"name = Wpf",那么这个设置不起作用是否有办法使其工作?我正在寻找最新的EF6而不是旧方法.

c# sql-server wpf entity-framework-6

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

如何在源代码管理资源管理器TFS 2012中强制手动合并?

我有一组主要的分支.在main回滚之后创建了一个分支,在此之前创建了另一个分支.现在尝试合并两个分支时,源代码控制会自动覆盖所有更改.如何强制它允许我手动比较和合并更改?

c# tfs visual-studio-2012

9
推荐指数
2
解决办法
8851
查看次数

如何让我的MultiSelectList绑定到我的数据模型?

我和汽车之间有许多关系.在我看来,我希望能够为灯光等项目提供多选列表.我尝试了很多不同的格式,我似乎无法获得列表来设置所选项目.我喜欢的方法是@Html.ListBoxFor(model => model.enitity.relatedentity,Model.SomeSelectListItemList),但是我似乎无法获取列表以在视图中设置所选项目.

下面是我测试的代码,我使用的是Asp.net MVC5,C#和实体框架6.

这是我的控制器动作

    // GET: Car/Edit/
    [Authorize(Roles = "Ecar")]
    public ActionResult Edit(int id)
    {


        CarsEditViewModel carEditViewModel = new CarsEditViewModel();

        carEditViewModel.Cars = unitOfWorkcar.CarRepository.FindIncluding(id);

        IList<Approval> approvalList = unitOfWorkcar.ApprovalRepository.All.ToList();
        IList<Connector> connectorList = unitOfWorkcar.ConnectorRepository.All.ToList();
        IList<InputVoltage> inputVoltagesList = unitOfWorkcar.InputVoltageRepository.All.ToList();



        carEditViewModel.ApprovalList = from c in approvalList select new SelectListItem { Text = c.Name, Value = c.Id.ToString(), Selected = true};

        carEditViewModel.Categories = new MultiSelectList(carEditViewModel.ApprovalList, "Value", "Text", "Selected");


       // ,carEditViewModel.ApprovalList.Select(c => c.Text),carEditViewModel.ApprovalList.Select(c => c.Selected)

        //carEditViewModel.ApprovalList = from c in approvalList select new MultiSelectList( //{ Selected …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc entity-framework entity-framework-6 asp.net-mvc-5

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

ListBoxItem使用的正确默认样式是什么,为什么Blend与MSDN不匹配?

使用在blend中找到的默认样式时,开发人员会在从列表中删除项目或在大型列表中滚动时从visual studio收到有关Horizo​​ntalContentAlignment和VerticalContentAlignment的警告.

或者,当您在Visual Studio中调用默认模板时,您会收到一个更简单的控件模板,该模板不包含可疑绑定.

但是,如果您查看C:\ Program Files(x86)\ Windows Kits\8.0\Include\WinRT\Xaml\Design\generic.xaml.你会发现一个更大更复杂的风格和模板,其中不包含可疑绑定.

MSDN也有一个样式,其模板类似于Windows工具包中的模板.

所以基本上我的假设是使用Windows Kit中的样式.我很困惑,虽然为什么混合会给出这样一个不同的模板以及为什么这两个绑定存在于它中,因为它们会抛出xaml警告?

以下是所有受尊重的样式和模板

混合默认样式和模板

<Window x:Class="WpfApplication17.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="Padding" Value="2,0,0,0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml

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

当 WPF 中的绑定为 null 时,如何避免 xaml 警告?

在我的 WPF 应用程序中,我有一个样式,当绑定对象不为空时,该样式用图像画笔填充矩形。该样式按照我预期的方式工作,但是当 Binding 为 null 时,我会收到有关 ImageBrush setter 值的警告。我希望我可以使用转换器或空触发器来避免这种情况,但是我仍然收到警告。有没有一种方法可以绑定到一个对象并检查它在 xaml 中是否为 null,而不会在输出窗口中收到警告?

\n\n
        <Style TargetType="Rectangle" x:Key="LightHeadRectangle">\n            <Setter  Property="Fill" Value="Red" />\n            <Style.Triggers>\n                <DataTrigger Binding="{Binding BarLight, Converter={StaticResource isNullConverter}}" Value="false">\n                    <Setter  Property="Fill">\n                        <Setter.Value>\n                            <ImageBrush Stretch="Fill" ImageSource="{Binding BarLight.OpenMount.ImagePath}" />\n                        </Setter.Value>\n                    </Setter>\n                </DataTrigger>\n            </Style.Triggers>\n        </Style>\xe2\x80\x8b\n
Run Code Online (Sandbox Code Playgroud)\n\n

更新:希望阻止通用空警告的发生,但在这种情况下我收到的具体警告是。

\n\n

System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。BindingExpression:Path=BarLight.OpenMount.ImagePath; 数据项=空;目标元素是“ImageBrush”(HashCode=47952502);目标属性是“ImageSource”(类型“ImageSource”)

\n\n

更新:

\n\n

请注意,我正在使用此样式来定位多个列表框项数据模板,这可能会有所帮助。BarLight 最初也是空的。

\n

c# wpf xaml

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

我应该如何链接到ASP.NET MVC 5中的用户配置文件页面?

我听说允许用户提交带有用户ID的表单并不是一个好的安全做法.这让我相信允许在url中看到用户id是不好的做法.但我需要一种方法来允许用户查看其他用户配置文件.

我想知道,而不是使用

@Html.ActionLink("Details", "Details", new { id = item.Id }) 
Run Code Online (Sandbox Code Playgroud)

如果我应该使用

@Html.ActionLink("Details", "Details", new { id = item.username }) 
Run Code Online (Sandbox Code Playgroud)

然后在控制器中按用户名查找用户并返回用户.

这是解决这个问题的正确方法吗?

这是我的链接

@Html.ActionLink(person.FullName, "Details","User" , new { UserName = person.UserName }, null)
Run Code Online (Sandbox Code Playgroud)

这是我的控制器动作

    // GET: /User/Details?UserName=bbob
    public ActionResult Details(string UserName)
    {
        if (UserName == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        ApplicationUser aspnetuser = UserManager.FindByName(UserName);

        if (aspnetuser == null)
        {
            return HttpNotFound();
        }
        return View(aspnetuser);
    }
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc asp.net-mvc-5

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

当我在MVC中调用GetType Name时,为什么会得到一组数字?

我正在使用c#Asp.net MVC.我有一个TPH模型,所以我调用GetType().Name来找出正在使用的类类型.但有时当我调用GetType().Name时,我不仅得到了模型的名称,还得到了一大串数字.为什么会发生这种情况我怎么能避免这种情况?

public string DiscriminatorValue
{
    get{ return this.GetType.Name;
}
//...
RedirectToAction("Index", 
                 "Files", 
                  new { itemId = vm.itemid, 
                        itemtype = vm.item.DiscriminatorValue.ToString()});
Run Code Online (Sandbox Code Playgroud)

得到这个:

DiscriminatorActualValue_D808C81C7BDE227500B30C6760AC934EA4A1307BD88500694065B3389D2642B1

c# asp.net-mvc entity-framework asp.net-mvc-4

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

如何从Asp.net MVC中的ListBoxFor选择框返回空列表而不是空列表?

控制器得到行动

IList<InputVoltage> inputVoltagesList = unitOfWorkPds.InputVoltageRepository.GetAll.ToList();

pdsEditViewModel.InputVoltageList = inputVoltagesList.Select(m => new SelectListItem { Text = m.Name, Value = m.Id.ToString() }); 
Run Code Online (Sandbox Code Playgroud)

视图模型

    public IEnumerable<SelectListItem> InputVoltageList { get; set; }
    public List<int> SelectedInputVoltages { get; set; }
Run Code Online (Sandbox Code Playgroud)

视图

    @Html.ListBoxFor(m => m.SelectedInputVoltages, Model.InputVoltageList)
Run Code Online (Sandbox Code Playgroud)

当用户没有选择selectedInputvoltages进入我的post控制器操作时,我希望收到一个空列表null如何让它作为空列表进入?

我喜欢这两个答案在使用其中一个有什么好处吗?

c# asp.net asp.net-mvc html.dropdownlistfor asp.net-mvc-4

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

如何使用样式和 DataTrigger 绑定到 XAML 中项目源的计数?

如果我按名称调用列表,我可以绑定到 Item 源,但是,我无法通过获取每个单独组合框上的项目计数来使绑定正常工作。

这是我想在 XAML 中做的事情。我需要更改什么才能使此绑定工作?

<Grid.Resources>
  <Style TargetType="ComboBox">                             
     <Style.Triggers>
       <DataTrigger Binding="{Binding Path=Items.Count}" Value="0">
          <Setter Property="IsEnabled" Value="False"></Setter>
       </DataTrigger>
     </Style.Triggers>
   </Style>
</Grid.Resources>

   <ComboBox
    ItemsSource="{Binding MyList}"               
    SelectedItem="{Binding SelectedElement}"
    ItemTemplate="{StaticResource MyTemplate}">
      </ComboBox>
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml

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

如何在WPF中失去焦点时触发文本框命令?

如果用户按下Enter,下面的模板工作正常,虽然我希望命令在TextBox失去焦点时触发.我怎样才能做到这一点?

以下是我的TextBox模板

<DataTemplate x:Key="PhantomNameEditTemplate">          
    <TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}">
        <TextBox.InputBindings>                 
            <KeyBinding Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}, Path=DataContext.RenameCommand}" CommandParameter="{Binding}" Key="Enter" />
        </TextBox.InputBindings>
    </TextBox>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml

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