我有一个存储过程,可以更改数据库中的大量数据.从应用程序调用此存储过程,同时使用EF进行数据操作.
所以我点击一个按钮,存储过程在数据库中运行,数据被更改,EF显示旧数据给用户.
有没有办法强制DbContext或ObjectContext刷新数据库中的数据?ObjectContext.Refresh()可能是解决方案,但我不想为每个可能更改的表调用此方法.我希望一次性刷新所有表格.
我正在使用Entity Framework 5,面向.NET 4.0
编辑:已添加数据,但EF不反映现有数据的修改.我看到新添加的记录,但我看不到我对现有记录所做的更改.
我正在尝试捕获在本地网络中提交给打印机的每个打印作业.我想显示工作的一些属性,如工作名称,提交时间等.
我尝试了一个while循环,但它没有抓住我的打印作业,也许是因为它在线程正在睡觉时发生了.是否有可以注册和处理的事件?我不想花费所有CPU资源来完成此任务的infinetly循环.
我试过这个:
public static void WritePrinterJobs()
{
while (true)
{
foreach (var job in LocalPrintServer.GetDefaultPrintQueue().GetPrintJobInfoCollection())
{
Console.WriteLine(job.Submitter + " " + job.TimeJobSubmitted.ToShortDateString());
}
Thread.Sleep(100);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑: 上面的代码实际上工作,你不需要更低级别,如果它适合你,我的错误是没有正确配置默认打印机.
我想用运行时创建的另一个实例更改注册的实例。
这可以删除现有组件并重新注册新组件,或者只是将新创建的实例重新分配给已注册的组件。
Foo old = new Foo("asd");
IoC.Register(Component.For<IFoo>().Instance(old));
Foo new = new Foo("qwe");
IoC.Unregister(old); // RemoveComponent method is removed from IKernel after v3.0
IoC.Register(Component.For<IFoo>().Instance(new));
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?请不要提出其他想法,例如“重新初始化您的 IoC 容器”等。
我有一个视图,其中包含来自多个表的数据.
当我调用该视图时,结果将加载到内存中并存储.在对应该影响视图的其他表进行一些更改后,视图对更改一无所知.
因此,当我再次调用该视图时,例如Get()方法,EF返回存储数据的值.
当然我想要更新的数据.如何强制视图从数据库而不是从内存中获取数据?还是有更好的策略?
如果我能让视图意识到正在进行的更改,那就更好了.这可以通过实体配置实现,也许可以通过使用HasRequired()方法来映射FK?
编辑: 我正在使用存储库和工作单元模式.所以我不是每次都创建和处理新的上下文.请考虑一下.
如果我最初预选多个 RadioButton,Android API Level 24(模拟器)似乎允许多选。我只想知道这是否是一个错误?
这是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/metal"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/classical"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/jazz"/>
</RadioGroup>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
该应用程序启动如下:
如果我点击 Jazz,它会变成这样:
这是否可以new {variable = x.something}动态创建和指定变量名称?例如:
var name = "dynamicName";
var result = context.select(x=> new {name.ToString() = x.something })
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我们将得到一个列表,其中属性名称为dynamicName.这有可能吗?
我有两个实体,定义如下
public class Corporation
{
public int Id{get;set;}
public string Name{get;set;}
public List<Location> Locations{get;set;} // All locations under this corp
}
public class Location
{
public int Id{get;set;}
public Corporation Corporation{get;set;} // Corporation is required in configuraion
}
Run Code Online (Sandbox Code Playgroud)
当我尝试添加公司然后添加位置时,我会定义两个公司.一个通过我的功能添加公司(这很好),一个通过添加位置的功能(这是问题).
位置添加功能如下:
public void AddLocation(int locationId)
{
using (Context context = new Context())
{
Location location = new Location();
location.Corporation = GetCorporationFromDb(corpId);
context.Locations.Add(location); // This one adds another same Corporation to DB
context.SaveChanges();
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么能避免这个?我必须在Location之前添加公司,因为在实施中,Location使用公司的数据库Id计算电子代码.
我读了很多文章和 StackOverflow 答案,但仍然无法理解为什么我们需要工厂方法来创建 Fragment 的实例。
以下 Fragment 类都可以正常工作。
具有两个构造函数的片段:
public class CtorFragment extends Fragment {
private static final String KEY = "the_key";
public CtorFragment() {
// Android calls the default constructor so default constructor must be explicitly defined.
// As we have another constructor, Android won't create a default constructor for us.
}
public CtorFragment(String s) {
// Set the arguments.
Bundle bundle = new Bundle();
bundle.putString(KEY, s);
setArguments(bundle);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle …Run Code Online (Sandbox Code Playgroud)