在我的视图模型中,我正在执行以下操作。
public List<Order> Orders { get; set; }
public ViewModel()
{
using (Context context = new Context())
Orders = context.Orders
.Include(order => order.Status).ToList();
}
Order test1 = Orders.First(item => item.Status != null);
Order test2 = Orders.First(item => item.Status != null && item.Status.Id == 1);
所以我已经包含了导航属性并将它们放在那里供将来使用。但是,如果我四处寻找并展开被观察的物体,我会发现状态实际上没有任何价值。它会产生以下错误。
'(test1.Status).Orders'抛出了'System.ObjectDisposedException'类型的异常
现在,我的理解是,如果我忘记使用 Include(),这就是结果,因为实体超出了上下文的范围并被处理掉了。但是在这种情况下,他们不是,我为什么会被打磨。
连连呢?我错过了什么?
在我开玩笑之后,我不能停止,但是这种印象是包含状态是正确的,但反过来,它不包括引用回到原订单。我对如何接近它感到有点困惑......
答案 0 :(得分:2)
此问题默认情况下context
LazyLoadingEnabled=true
有Include
,因此Status
不会为每个热切加载的using (Context context = new Context()){
context.Configuration.LazyLoadingEnabled = false;
Orders = context.Orders
.Include(order => order.Status).ToList();
}
设置后向引用。您可以尝试关闭该上下文的延迟加载,并且您将看到后向引用也将被正确加载:
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_ingredients) {
Intent intent = new Intent(getApplicationContext(),IngredientsActivity.class);
startActivity(intent);
} else if (id == R.id.nav_recepies) {
} else if (id == R.id.nav_grocery_list) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
我刚做了一个简单的演示并确认它有效。