我正在尝试使用带有内置用户身份验证的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
如何以编程方式在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而不是旧方法.
我有一组主要的分支.在main回滚之后创建了一个分支,在此之前创建了另一个分支.现在尝试合并两个分支时,源代码控制会自动覆盖所有更改.如何强制它允许我手动比较和合并更改?
我和汽车之间有许多关系.在我看来,我希望能够为灯光等项目提供多选列表.我尝试了很多不同的格式,我似乎无法获得列表来设置所选项目.我喜欢的方法是@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
使用在blend中找到的默认样式时,开发人员会在从列表中删除项目或在大型列表中滚动时从visual studio收到有关HorizontalContentAlignment和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) 在我的 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\nSystem.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。BindingExpression:Path=BarLight.OpenMount.ImagePath; 数据项=空;目标元素是“ImageBrush”(HashCode=47952502);目标属性是“ImageSource”(类型“ImageSource”)
\n\n更新:
\n\n请注意,我正在使用此样式来定位多个列表框项数据模板,这可能会有所帮助。BarLight 最初也是空的。
\n我听说允许用户提交带有用户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) 我正在使用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
控制器得到行动
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如何让它作为空列表进入?
我喜欢这两个答案在使用其中一个有什么好处吗?
如果我按名称调用列表,我可以绑定到 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) 如果用户按下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)